简体   繁体   English

OpenCV Python:仅在ROI中检测行

[英]OpenCV Python: Detecting lines only in ROI

I'd like to detect lines inside a region of interest. 我想检测感兴趣区域内的线条。 My output image should display the original image and the detected lines in the selected ROI. 我的输出图像应显示原始图像和所选ROI中检测到的线条。 So far it has not been a problem to find lines in the original image or select a ROI but finding lines only inside the ROI did not work. 到目前为止,在原始图像中找到线条或选择ROI并不是问题,但是仅在ROI内部查找线条是行不通的。 My MWE reads an image, converts it to grayscale and lets me select a ROI but gives an error when HoughLinesP wants to read roi . 我的MWE读取图像,将其转换为灰度,并让我选择ROI,但当HoughLinesP要读取roi时出现错误。

import cv2
import numpy as np

img = cv2.imread('example.jpg',1)
gray = cv2.cvtColor(img ,cv2.COLOR_BGR2GRAY)

# Select ROI
fromCenter = False
roi = cv2.selectROI(gray, fromCenter)

# Crop ROI
roi = img[int(roi[1]):int(roi[1]+roi[3]), int(roi[0]):int(roi[0]+roi[2])]

# Find lines
minLineLength = 100
maxLineGap = 30
lines = cv2.HoughLinesP(roi,1,np.pi/180,100,minLineLength,maxLineGap)
for x in range(0, len(lines)):
    for x1,y1,x2,y2 in lines[x]:
        cv2.line(img,(x1,y1),(x2,y2),(237,149,100),2)

cv2.imshow('Image',img)
cv2.waitKey(0) & 0xFF

cv2.destroyAllWindows()

The console shows: 控制台显示:

lines = cv2.HoughLinesP(roi,1,np.pi/180,100,minLineLength,maxLineGap) 行= cv2.HoughLinesP(roi,1,np.pi / 180,100,minLineLength,maxLineGap)

error: OpenCV(3.4.1) C:\\Miniconda3\\conda-bld\\opencv-suite_1533128839831\\work\\modules\\imgproc\\src\\hough.cpp:441: error: (-215) image.type() == (((0) & ((1 << 3) - 1)) + (((1)-1) << 3)) in function cv::HoughLinesProbabilistic 错误:OpenCV(3.4.1)C:\\ Miniconda3 \\ conda-bld \\ opencv-suite_1533128839831 \\ work \\ modules \\ imgproc \\ src \\ hough.cpp:441:错误:(-215)image.type()==((函数cv :: HoughLinesProbabilistic中的(0)&((1 << 3)-1))+((((1)-1)<< 3))

My assumption is that roi does not have the correct format. 我的假设是roi格式不正确。 I am using Python 3.6 with Spyder 3.2.8. 我在Spyder 3.2.8中使用Python 3.6。 Thanks for any help! 谢谢你的帮助!

The function cv2.HoughLinesP is expecting a single-channel image, so the cropped region could be taken from the gray image and that would remove the error: 函数cv2.HoughLinesP期望使用单通道图像,因此可以从灰色图像中获取裁剪区域,这样可以消除错误:

# Crop the image
roi = list(map(int, roi)) # Convert to int for simplicity
cropped = gray[roi[1]:roi[1]+roi[3], roi[0]:roi[0]+roi[2]]

Note that I'm changing the output name from roi to cropped , and that's because you're going to still need the roi box. 请注意,我将输出名称从roi更改为cropped ,这是因为您仍然需要roi框。 The points x1 , x2 , y1 , and y2 are pixel positions in the cropped image, not the full image. x1x2y1y2是裁剪图像中的像素位置,而不是完整图像中的像素位置。 To get the images drawn correctly, you can just add the upper left corner pixel position from roi . 要正确绘制图像,您只需添加roi左上角像素位置。 Here's the for loop with relevant edits: 这是带有相关修改的for循环:

# Find lines
minLineLength = 100
maxLineGap = 30
lines = cv2.HoughLinesP(cropped,1,np.pi/180,100,minLineLength,maxLineGap)
for x in range(0, len(lines)):
    for x1,y1,x2,y2 in lines[x]:
        cv2.line(img,(x1+roi[0],y1+roi[1]),(x2+roi[0],y2+roi[1]),(237,149,100),2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM