在图像中搜索矩形(硬盘)

时间:2016-03-30 08:56:43

标签: python python-2.7 opencv image-processing

任务:找到图像中的硬盘以确定其角度和轮廓

问题:无法始终找到正确的光盘轮廓。

在代码中,我将图像设置为灰色,模糊,找到它们之间的差异并定义轮廓,但解决方案并不精确。我想帮助你。

第二个问题: 也许我可以从这张图片中检测到我的磁盘?你能用算法帮我吗?

背景图片:

enter image description here

来源图片:

enter image description here

firstFrame = cv2.imread(bg_im)  # background image
frame_img = cv2.imread(frame)   # source image

gray = cv2.cvtColor(frame_img, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (21, 21), 0)
firstFrame = cv2.cvtColor(firstFrame, cv2.COLOR_BGR2GRAY)
firstFrame = cv2.GaussianBlur(firstFrame, (21, 21), 0)

frameDelta = cv2.absdiff(firstFrame, gray)
thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1]
thresh = cv2.dilate(thresh, None, iterations=2)
(cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

c = sorted(cnts, key = cv2.contourArea, reverse = True)[0]

rect = cv2.minAreaRect(c)
box = cv2.cv.BoxPoints(rect)

x1 = box[0][0]
x2 = box[1][0]
x4 = box[3][0]

y1 = box[0][1]
y2 = box[1][1]
y4 = box[3][1]

perimeter_1_4 = sqrt((x4 - x1)**2 + (y1 - y4)**2)
perimeter_1_2 = sqrt((x2 - x1)**2 + (y1 - y2)**2)

box = np.int0(box)
corner = list(rect)[2]

if perimeter_1_4 > perimeter_1_2:
    corner = -(90 - corner)    

cv2.drawContours(frame_img,[box],0,(0,0,255),2)

0 个答案:

没有答案