PythonOpenCV2:在图像中查找最大的blob会提供两种不同的输出
PythonOpenCV2:在图像中查找最大的blob会提供两种不同的输出,python,image,opencv,contour,Python,Image,Opencv,Contour,我正在使用python opencv2查找图像中最大的blob。虽然我将Image1作为输入,但它给出了正确的输出,但当我尝试使用另一个Image2时,相同的代码给出了错误的输出。两个输入图像给出不同的阈值图像作为输出。我希望问题就在这里。实际上我犯了什么错误?提前谢谢 输入图像屏幕截图:- 输出图像屏幕截图:- 红色矩形标记的对象是目标。Image1正确检测到对象,但Image2错误检测到对象 代码:- import cv2 import numpy as np def do(im):
我正在使用python opencv2查找图像中最大的blob。虽然我将Image1作为输入,但它给出了正确的输出,但当我尝试使用另一个Image2时,相同的代码给出了错误的输出。两个输入图像给出不同的阈值图像作为输出。我希望问题就在这里。实际上我犯了什么错误?提前谢谢
输入图像屏幕截图:-
输出图像屏幕截图:-
红色矩形标记的对象是目标。Image1正确检测到对象,但Image2错误检测到对象 代码:-
import cv2
import numpy as np
def do(im):
im1 = im
im = cv2.cvtColor(im,cv2.COLOR_RGB2GRAY)
cv2.imwrite('01gray.jpg',im)
ret,thresh = cv2.threshold(im,127,255,0)
cv2.imwrite('02thresh.jpg',thresh)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(im1,contours,-1,(0,255,0),3)
cv2.imshow('test',im1)
cv2.imwrite('03test.png',im1)
#test
max_area = 0
best_cnt = None
for cnt in contours:
area = cv2.contourArea(cnt)
if area > max_area:
max_area = area
best_cnt = cnt
#print best_cnt
# finding centroids of best_cnt and draw a circle there
M = cv2.moments(best_cnt)
cx,cy = int(M['m10']/M['m00']), int(M['m01']/M['m00'])
print cx,cy
if best_cnt is not None:
x,y,w,h = cv2.boundingRect(best_cnt)
cv2.rectangle(im1, (x,y),(x+w,y+h), (0,0,255), 3)
cv2.imwrite('04test1.png',im1)
im = cv2.imread('Image1.jpeg') #Works good
#im = cv2.imread('Image2.png') #Problem with that Image
do(im);
你不应该对这两幅图像使用相同的阈值,因为阈值将为你提供第一幅图像中的水滴(白色)和第二幅图像(不是飞机)中的背景(天空)的输出。是的,Ted W先生。你是对的
现在,我使用otsu方法为每个输入图像查找阈值,如下所示
(automatic_thresh_value_calc, im_bw) = cv2.threshold(input_gray_img, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
ret,thresh = cv2.threshold(input_gray_img,automatic_thresh_value_calc,255,0)
cv2.imwrite('thresholdedImage.jpg',input_gray_img)
我说得对吗?否则,在python中是否有找到自动阈值的好方法
谢谢…定义“正确的输出”。这很可能是阈值的问题,所以请添加一些阈值图像。我无法放置所有图像,因为该网站说“你需要更多的声誉”。所以,我只是更新了它的截图。请帮忙。非常感谢。