将图像坐标转换为矩阵索引

时间:2017-12-19 20:26:33

标签: python image python-3.x opencv image-processing

我试图从图像中删除blob(Python3)。问题是我在笛卡尔坐标中得到斑点的位置。我的目的是获取blob在矩阵索引中的位置,这样我就可以访问它们并将它们删除(使它们等于零)。

这是我的代码:

<!DOCTYPE html>
<html>

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script>
  <link href="https://netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
   
</head>

<body ng-app="myApp" ng-controller="myCtrl as vm">
  <div class="container">
    <h3>AUTHORS' LIST</h3>
    <div class="btn-group">
      <button ng-click="addAuthor()" type="button" class="btn btn-default">Add</button>
      <button ng-click="SavelocalStorage()" type="button" class="btn btn-default">Save</button>
    </div>
    <form ng-controller="booksCtrl" name="vm.myForm">
      <table class="table table-bordered">
        <tr>
          <th>Surname</th>
          <th>Name</th>
          <th>Books</th>
        </tr>
        <tr ng-repeat="author in authors">
          <td><input ng-model="author.surname" required type="text" class="form-control"></td>
          <td><input ng-model="author.name" required type="text" class="form-control"></td>
          <td>
            <button ng-click="showBookList(author)" type="button" class="btn btn-default">List</button>
          </td>
        </tr>
      </table>

      <div ng-show="showBooks" class="col-sm-8" style="background-color:lightblue; position: absolute; left:5px; top:5px;z-index:2;">
        <div class="btn-group">
          <button ng-click="addBook()" type="button" class="btn btn-default">Add</button>
        </div>
        <table class="table table-bordered">
          <tr>
            <th>Title</th>
          </tr>
          <tr ng-repeat="book in currentAuthor.books">
            <td><input ng-model="book.title" type="text" class="form-control"></td>
          </tr>
        </table>
        <button class="btn btn-sm btn-warning" ng-click="showBooks = false">Close</button>
      </div>
    </form>
  </div>
      </body>
</html>    

这是我要清理的图像: enter image description here

关键点是笛卡尔坐标中的内容。我想要的是将其转换为矩阵索引,以便我可以访问图像median2上的相同索引,然后删除blob。

1 个答案:

答案 0 :(得分:1)

(1)语法错误:

您的代码:

for j in range(1, len(keypoints)):
    x = keypoints[j].pt[0] #i is the index of the blob you want to get the position
    y = keypoints[j].pt[1]
    median2[x, y] = 0

众所周知,关键点是浮点数,而索引应该是int;我们用[rows,cols]索引图像,即[y,x]。

改性:

for kpt in keypoints:
    x = int(pt[0])     # change float to int 
    y = int(pt[1])
    median2[y, x] = 0  # index in [row,col] order, that is (y,x)

(2)逻辑错误

您无法通过将blob关键点设置为零来删除小区域。