简体   繁体   English

Python Open CV-获取区域坐标

[英]Python Open CV - Get coordinates of region

I am a beginner in image processing (and openCV). 我是图像处理(和openCV)的初学者。 After applying watershed algorithm to an image, the output that is obtained is something like this - 在对图像应用分水岭算法后,获得的输出如下所示:

在此处输入图片说明

Is it possible to have the co-ordinates of the regions segmented out ? 是否可以将区域的坐标分割开?

The code used is this (in case you wish to have a look) - 使用的代码是这样的(如果您希望看一下)-

import numpy as np
import cv2
from matplotlib import pyplot as plt


img = cv2.imread('input.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)

# noise removal
kernel = np.ones((3,3),np.uint8)
opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 2)

# sure background area
sure_bg = cv2.dilate(opening,kernel,iterations=3)

# Finding sure foreground area
dist_transform = cv2.distanceTransform(opening,cv2.cv.CV_DIST_L2,5)
ret, sure_fg = cv2.threshold(dist_transform,0.7*dist_transform.max(),255,0)

# Finding unknown region
sure_fg = np.uint8(sure_fg)
unknown = cv2.subtract(sure_bg,sure_fg)

# Marker labelling
ret, markers = cv2.connectedComponents(sure_fg)

# Add one to all labels so that sure background is not 0, but 1
markers = markers+1

# Now, mark the region of unknown with zero
markers[unknown==255] = 0

markers = cv2.watershed(img,markers)
img[markers == -1] = [255,0,0]

plt.imshow(img)
plt.show()

Is there any function or algorithm to extract the co-ordinates of the coloured regions that are separated out ? 是否有任何函数或算法可以提取分离出的有色区域的坐标? Any help would be much appreciated ! 任何帮助将非常感激 !

After this line: 在此行之后:

markers = cv2.watershed(img,markers)

markers will be an image with all region segmented, and the pixel value in each region will be an integer ( label ) greater than 0. Background has label 0, boundaries has label -1. markers将是对所有区域进行分割的图像,并且每个区域中的像素值将是一个大于0的整数( 标签 )。背景的标签为0,边界的标签为-1。

You already know the number of labels from ret returned by connectedComponents . 您已经知道connectedComponentsret返回的标签数。

You need a data structure to contains the points for each region. 您需要一个数据结构来包含每个区域的点。 For example, the points of each region will go in an array of points. 例如,每个区域的点将以点的形式排列。 You need several of this (for each region), so another array. 您需要几个(每个区域),所以需要另一个数组。

So, if you want to find the pixel of each region, you can do: 因此,如果要查找每个区域的像素,可以执行以下操作:

1) Scan the image and append the point to an array of arrays of points, where each array of points will contains the points of the same region 1)扫描图像并将该点附加到点数组组成的数组中,其中每个点数组将包含相同区域的点

// Pseudocode

"labels" is an array of an array of points
initialize labels size to "ret", the length of each array of points is 0.

for r = 1 : markers.rows
    for c = 1 : markers.cols
        value = markers(r,c) 
        if(value > 0)
            labels{value-1}.append(Point(c,r)) // r = y, c = x
        end
    end
end

2) Generate a mask for each label value, and collect the points in the mask 2)为每个标签值生成一个掩码,并收集掩码中的点

// Pseudocode

"labels" is an array of an array of points
initialize labels size to "ret", the length of each array of points is 0.

for value = 1 : ret-1
    mask = (markers == value)
    labels{value-1} = all points in the mask // You can use cv::boxPoints(...) for this
end

The first approach is likely to be much faster, the second is easier to implement. 第一种方法可能会快得多,第二种方法更容易实现。 Sorry, but I can't give you Python code (C++ would have been much better :D ), but you should find your way out whit this. 抱歉,但是我不能给您Python代码(C ++会更好:D),但是您应该找到解决方法。

Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM