Numpy 如何使用ROI从所需的点绘制矩形?
你好,我是OpenCv的初学者。 我有一个迷宫的图像。我写了迷宫解算器代码。我需要得到像这样的代码工作图片的照片。 我想使用ROI选择白色区域的轮廓,但我不能 当我尝试ROI方法时,我得到一个选择了黑色区域的平滑矩形 ----->这是我的代码结果 我想知道这个结果Numpy 如何使用ROI从所需的点绘制矩形?,numpy,opencv,roi,Numpy,Opencv,Roi,你好,我是OpenCv的初学者。 我有一个迷宫的图像。我写了迷宫解算器代码。我需要得到像这样的代码工作图片的照片。 我想使用ROI选择白色区域的轮廓,但我不能 当我尝试ROI方法时,我得到一个选择了黑色区域的平滑矩形 ----->这是我的代码结果 我想知道这个结果 import cv2 import numpy as np #import image image = cv2.imread('rt4.png') #grayscaleqq gray = cv2.cvtColor(image,cv
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])
绘制倾斜矩形的一个简单解决方案是使用
cv2.polylines
。根据你的结果,我假设你已经有了这个区域顶点的坐标,我们把它们叫做[x1,y1],[x2,y2],[x3,y3],[x4,y4]。“多段线”功能从一个顶点到另一个顶点绘制一条线,以创建闭合多边形
导入cv2
将numpy作为np导入
#以数组形式列出顶点的坐标
pts=np.array([[x1,y1],[x2,y2],[x3,y3],[x4,y4]],np.int32)
pts=pts.重塑(-1,1,2))
#从一个顶点到另一个顶点画线
cv2.多段线(图像,[pts],真,(255,0,0))
我之前误解了你的问题。所以,我在重写
正如@消音器所述,您可以使用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()
我在下图中使用了它:
我得到了正确的答案。您可以添加额外的过滤器,或者使用ROI减少面积,以减少差异,但这不是必需的
希望有帮助 如果你对图像设置阈值并在其上找到轮廓,然后绘制轮廓,你应该得到第二个。你能再解释一下吗?嗨,首先谢谢你的回答。我没有角点。我只有ROI矩形点,我不知道如何获得角点。我是opencv的初学者。您知道如何获取吗?谢谢您的帮助,但我无法使用cv2.selectROI,因为代码自动选择了矩形我的错误!我没有完全理解这个问题。我会在某个时候更新我的答案。。道歉!我的英语不好,对不起:(谢谢你的帮助,我有一个假设。我需要全白色的轮廓。你的代码给了我一点外部的白色正在绘制框架。我解决了问题,只需在
gaus
上运行侵蚀,而不是在mask1
上运行侵蚀,也就是说,根本不执行膨胀。它现在工作得很好。运行它,让我知道它是否工作:)