Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 按位_和两个图像并计算结果对象_Python_Opencv_Image Processing_Opencv Contour - Fatal编程技术网

Python 按位_和两个图像并计算结果对象

Python 按位_和两个图像并计算结果对象,python,opencv,image-processing,opencv-contour,Python,Opencv,Image Processing,Opencv Contour,我试图得到正确答案的数量,给出一个模型答案样本和一个答案表示例,所以我使用了cv2.bitwise_和函数,然后我对结果图像进行腐蚀和膨胀,以计算代表正确答案的对象,但效果不好。 下面是我正在使用的两个示例图像: 这就是结果: 因此它检测到3个圆而不是2个。我试图改变腐蚀和膨胀的迭代次数,改变StructuringElement的形状,但仍然得到了错误的答案。 这是我的密码: import numpy as np import cv2 from PIL import Image img1

我试图得到正确答案的数量,给出一个模型答案样本和一个答案表示例,所以我使用了
cv2.bitwise_和
函数,然后我对结果图像进行腐蚀和膨胀,以计算代表正确答案的对象,但效果不好。 下面是我正在使用的两个示例图像:

这就是结果:

因此它检测到3个圆而不是2个。我试图改变腐蚀和膨胀的迭代次数,改变
StructuringElement
的形状,但仍然得到了错误的答案。 这是我的密码:

import numpy as np
import cv2
from PIL import Image 
img1 = cv2.imread("model_c3.png")
img2 = cv2.imread("ex_c3.png")

retval1, img1 = cv2.threshold(img1,225,255,cv2.THRESH_BINARY_INV)
retval2, img2 = cv2.threshold(img2,225,255,cv2.THRESH_BINARY_INV)
img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
img1gray = cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY)

mask = cv2.bitwise_and(img1gray, img2gray,img2)

el = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
e = cv2.erode(mask, el, iterations=2)
d = cv2.dilate(e, el, iterations=7)

im, contours, hierarchy = cv2.findContours(
    d,
    cv2.RETR_LIST,
    cv2.CHAIN_APPROX_SIMPLE
)

centers = []
radii = []
for contour in contours:    
    br = cv2.boundingRect(contour)

    m = cv2.moments(contour)
    center = (int(m['m10'] / m['m00']), int(m['m01'] / m['m00']))
    centers.append(center)

print("There are {} circles".format(len(centers)))    


cv2.imshow('result.png',d)
cv2.waitKey(0)
cv2.destroyAllWindows()

假阳性斑点似乎比其他斑点小。按区域过滤轮廓

area = cv2.contourArea(contour)

我更改了代码中的一些内容

对RGB图像设置阈值,然后将其转换为灰色,这没有多大意义。 不要对两幅图像进行and运算,然后进行形态学运算来清理结果,而应该简单地为正确的and运算准备两幅图像。使一个图像中的圆圈变大,另一个图像中的圆圈变小。通过这种方式,您还可以补偿两个图像之间的微小偏移(和结果中所有瑕疵的原因)

我不确定你是否完全理解你使用的函数,因为你使用它们的方式很奇怪,参数也很奇怪

请注意,我只是对代码中的一些内容进行了更改,以使其正常工作。这并不是一个完美的解决方案


我试过了,但并不是所有其他图像中的真阳性斑点都比假阳性斑点大。这增加了一些图像中的错误
import numpy as np
import cv2
#from PIL import Image
img1 = cv2.imread("d://1.png")
img2 = cv2.imread("d://2.png")

img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
img1gray = cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY)
retval1, img1bin = cv2.threshold(img1gray,128,255,cv2.THRESH_BINARY_INV)
retval2, img2bin = cv2.threshold(img2gray,128,255,cv2.THRESH_BINARY_INV)

el = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
e = cv2.erode(img1bin, el, iterations=2)
cv2.imshow("e", e)
d = cv2.dilate(img2bin, el, iterations=7)
result = cv2.bitwise_and(e, d)
cv2.imshow("d", d)

im, contours, hierarchy = cv2.findContours(
    result,
    cv2.RETR_LIST,
    cv2.CHAIN_APPROX_SIMPLE
)

centers = []
radii = []
for contour in contours:
    br = cv2.boundingRect(contour)

    m = cv2.moments(contour)
    center = (int(m['m10'] / m['m00']), int(m['m01'] / m['m00']))
    centers.append(center)

print("There are {} circles".format(len(centers)))


cv2.imshow('result',result)
cv2.waitKey(0)
cv2.destroyAllWindows()