我是OpenCV的新手。我想从图像中提取主要对象。所以,我在图像上应用了Canny以获取主对象周围的边缘并得到以下输出:
以下是在Python中使用OpenCV获取此代码的代码:
img = cv2.imread(file)
cv2.imshow("orig", img)
cv2.waitKey(0)
img = cv2.blur(img,(2,2))
gray_seg = cv2.Canny(img, 0, 50)
现在,我想在获取图像中的主要对象后,将下面的图像作为最终输出:
我想以优化的方式进行,因为我必须处理超过250万张图像。 任何人都可以帮我这个吗?
答案 0 :(得分:1)
rect函数应该提供您需要的功能。下面是一个如何使用它的例子。
cv::Mat image(img);
cv::Rect myROI(posX, posY, sizeX, sizeY);
cv::Mat croppedImage = image(myROI);
这是用c ++编写的,但应该能够找到python等价物。
我下面的链接提供了更多信息 How to crop a CvMat in OpenCV?
答案 1 :(得分:1)
如您找到--start-address=<section_start + function_start>
--stop-address=<section_start + function_start + function_size>
,然后要裁剪矩形区域,您应该计算clean canny edge
。
步骤:
结果:
要计算canny区域边界,您可以找到非零点,然后获取rectangle boundary
。使用NumPy轻松实现。然后使用切片操作进行裁剪。
min-max coords
当然,点上的#!/usr/bin/python3
# 2018.01.20 15:18:36 CST
#!/usr/bin/python3
# 2018.01.20 15:18:36 CST
import cv2
import numpy as np
#img = cv2.imread("test.png")
img = cv2.imread("img02.png")
blurred = cv2.blur(img, (3,3))
canny = cv2.Canny(blurred, 50, 200)
## find the non-zero min-max coords of canny
pts = np.argwhere(canny>0)
y1,x1 = pts.min(axis=0)
y2,x2 = pts.max(axis=0)
## crop the region
cropped = img[y1:y2, x1:x2]
cv2.imwrite("cropped.png", cropped)
tagged = cv2.rectangle(img.copy(), (x1,y1), (x2,y2), (0,255,0), 3, cv2.LINE_AA)
cv2.imshow("tagged", tagged)
cv2.waitKey()
也可以。