如何使用ROI从我想要的点绘制一个矩形?

问题描述 投票:-1回答:2

您好,我是OpenCv的初学者。我有一个迷宫图像。我写了迷宫求解器代码。我需要得到像这张照片的照片才能使用此代码。我想用ROI选择白色区域的轮廓,但我不能

当我尝试ROI方法时,我得到一个选择了黑色区域的平滑矩形。

https://i.stack.imgur.com/Ty5BX.png ----->这是我的代码结果

https://i.stack.imgur.com/S7zuJ.png -------->我想要这个结果

import cv2
import numpy as np

#import image
image = cv2.imread('rt4.png')

#grayscaleqq
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
#cv2.imshow('gray', gray)
#qcv2.waitKey(0)

#binary
#ret,thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY_INV)
threshold = 150
thresh = cv2.threshold(gray, threshold, 255, cv2.THRESH_BINARY)[1]
cv2.namedWindow('second', cv2.WINDOW_NORMAL)
cv2.imshow('second', thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()

#dilation
kernel = np.ones((1,1), np.uint8)
img_dilation = cv2.dilate(thresh, kernel, iterations=1)
cv2.namedWindow('dilated', cv2.WINDOW_NORMAL)
cv2.imshow('dilated', img_dilation)
cv2.waitKey(0)
cv2.destroyAllWindows()




#find contours
im2,ctrs, hier = cv2.findContours(img_dilation.copy(), 
cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)


#sort contours
sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr) 
[0])

list = []

for i, ctr in enumerate(sorted_ctrs):
# Get bounding box
x, y, w, h = cv2.boundingRect(ctr)

# Getting ROI
roi = image[y:y+h, x:x+w]

a = w-x
b = h-y
list.append((a,b,x,y,w,h))


# show ROI
#cv2.imshow('segment no:'+str(i),roi)
cv2.rectangle(image,(x,y),( x + w, y + h ),(0,255,0),2)
#cv2.waitKey(0)

if w > 15 and h > 15:
    cv2.imwrite('home/Desktop/output/{}.png'.format(i), roi)

cv2.namedWindow('marked areas', cv2.WINDOW_NORMAL)
cv2.imshow('marked areas',image)
cv2.waitKey(0)
cv2.destroyAllWindows()




gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

gray = np.float32(gray)
dst = cv2.cornerHarris(gray,2,3,0.04)

#result is dilated for marking the corners, not important
dst = cv2.dilate(dst,None)


image[dst>0.01*dst.max()]=[0,0,255]

cv2.imshow('dst',image)
if cv2.waitKey(0) & 0xff == 27:
cv2.destroyAllWindows()

list.sort()
print(list[len(list)-1])
numpy opencv roi
2个回答
0
投票

我之前误解了你的问题。所以,我正在改写。

正如@Silencer已经说过的那样,你可以使用drawContours方法。你可以这样做:

import cv2
import numpy as np

#import image
im = cv2.imread('Maze2.png')
gaus = cv2.GaussianBlur(im, (5, 5), 1)
# mask1 = cv2.dilate(gaus, np.ones((15, 15), np.uint8, 3))
mask2 = cv2.erode(gaus, np.ones((5, 5), np.uint8, 1))
imgray = cv2.cvtColor(mask2, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray, 127, 255, 0)
im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

maxArea1=0
maxI1=0

for i in range(len(contours)):
    area = cv2.contourArea(contours[i])
    epsilon = 0.01 * cv2.arcLength(contours[i], True)
    approx = cv2.approxPolyDP(contours[i], epsilon, True)
    if area > maxArea1 :
        maxArea1 = area

print(maxArea1)
print(maxI1)
cv2.drawContours(im, contours, maxI1, (0,255,255), 3)

cv2.imshow("yay",im)
cv2.imshow("gray",imgray)
cv2.waitKey(0)

cv2.destroyAllWindows()

我在下面的图像中使用它:enter image description here我得到了正确的答案。您可以添加其他过滤器,或者可以使用ROI减少区域,以减少差异,但不是必需的

希望能帮助到你!


0
投票

只绘制一个倾斜的矩形的简单解决方案是使用cv2.polylines。根据你的结果,我假设你已经有了该区域顶点的坐标,让我们调用它们[x1,y1],[x2,y2],[x3,y3],[x4,y4]。折线功能从顶点到顶点绘制一条线以创建闭合多边形。

import cv2
import numpy as np

#List coordinates of vertices as an array
pts = np.array([[x1,y1],[x2,y2],[x3,y3],[x4,y4]], np.int32)
pts = pts.reshape((-1,1,2))

#Draw lines from vertex to vertex
cv2.polylines(image, [pts], True, (255,0,0))
© www.soinside.com 2019 - 2024. All rights reserved.