我正在开发一个个人项目,在该项目中,我检测矩形(尺寸相同),然后将这些矩形以相同的顺序(从上到下)放置在列表中,然后使用某些函数处理每个矩形内的信息。下面是我的测试图像。
我已经设法检测到感兴趣的矩形,但是我不断得到我不想要的其他矩形。如您所见,我只想将带有信息 (6,9,3) 的三个矩形放入列表中。
我的代码
import cv2
width=700
height=700
y1=0
y2=700
x1=500
x2=700
img=cv2.imread('test.jpg') #read image
img=cv2.resize(img,(width,height)) #resize image
roi = img[y1:y2, x1:x2] #region of interest i.e where the rectangles will be
gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY) #convert roi into gray
Blur=cv2.GaussianBlur(gray,(5,5),1) #apply blur to roi
Canny=cv2.Canny(Blur,10,50) #apply canny to roi
#Find my contours
contours =cv2.findContours(Canny,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)[0]
#Loop through my contours to find rectangles and put them in a list, so i can view them individually later.
cntrRect = []
for i …Run Code Online (Sandbox Code Playgroud) 我正在尝试学习图像特征检测技术。
我已经设法检测到水平线(不间断/连续),但是我无法检测图像中的所有虚线/断线。
这是我的测试图像,您可以看到有虚线和一些文本/框等。
到目前为止,我使用了以下代码,仅检测到一条虚线。
import cv2
import numpy as np
img=cv2.imread('test.jpg')
img=functions.image_resize(img,1000,1000) #function from a script to resize image to fit my screen
imgGray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
imgEdges=cv2.Canny(imgGray,100,250)
imgLines= cv2.HoughLinesP(imgEdges,2,np.pi/100,60, minLineLength = 10, maxLineGap = 100)
for x1,y1,x2,y2 in imgLines[0]:
cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)
cv2.imshow('Final Image with dotted Lines detected',img)
Run Code Online (Sandbox Code Playgroud)
我的输出图像如下。正如你所看到的,我只能检测到最后一条虚线。我已经尝试过参数 rho、theta、min/max 线,但没有运气。
任何意见是极大的赞赏 :)
python opencv image-processing feature-extraction python-3.x
我对检测线条(我设法使用霍夫变换找出)及其上面的文本感兴趣。
我写的代码如下。(我已经编辑,以便我可以循环遍历每行的坐标)
import cv2
import numpy as np
img=cv2.imread('test3.jpg')
#img=cv2.resize(img,(500,500))
imgGray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
imgEdges=cv2.Canny(imgGray,100,250)
imgLines= cv2.HoughLinesP(imgEdges,1,np.pi/180,230, minLineLength = 700, maxLineGap = 100)
imgLinesList= list(imgLines)
a,b,c=imgLines.shape
line_coords_list = []
for i in range(a):
line_coords_list.append([(int(imgLines[i][0][0]), int(imgLines[i][0][1])), (int(imgLines[i][0][2]), int(imgLines[i][0][3]))])
print(line_coords_list)#[[(85, 523), (964, 523)], [(85, 115), (964, 115)], [(85, 360), (964, 360)], [(85, 441), (964, 441)], [(85, 278), (964, 278)], [(85, 197), (964, 197)]]
roi= img[int(line_coords_list[0][0][1]): int(line_coords_list[0][1][1]), int(line_coords_list[0][0][0]) : int(line_coords_list[0][1][0])]
print(roi) # why does this print an empty list?
cv2.imshow('Roi NEW',roi)
Run Code Online (Sandbox Code Playgroud)
现在我只是不知道如何检测这些线上方的感兴趣区域。是否可以说裁剪出每一行并让图像显示 roi_1 、 roi_2 …