如何在python中执行图像感兴趣区域的自动检测和裁剪?

2024-09-28 21:33:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我想对我感兴趣的区域执行操作..这是中央矩形表,您可以在图像中看到。 我可以手动给出我感兴趣区域的坐标,并裁剪该部分 img = cv2.imread('test12.jpg',0) box = img[753:1915,460:1315]

但我想自动裁剪这部分,而不需要手动给出像素或坐标。有人能帮我一下吗?

http://picpaste.com/test12_-_Copy-BXqHMAnd.jpg这是我的原始图像。

http://picpaste.com/boxdemo-zHz57dBM.jpg这是我剪下的图像。 为此,我输入了所需区域的坐标并裁剪。 但是,现在我必须处理许多类似的图像,其中我感兴趣的区域的坐标将略有变化。我想要一种方法来检测表(我感兴趣的区域)并裁剪它。 现在我用这个 img = cv2.imread('test12.jpg',0) box = img[753:1915,460:1315] 裁剪我的图像。


Tags: 图像comboxhttp区域img手动cv2
2条回答

您可以尝试使用openCV模板匹配来查找图像中矩形表的坐标。 Template Matching

下面是一个测试程序,用于查找我试图查找的图像的坐标。

from __future__ import print_function
import cv2
import numpy as np
from matplotlib import pyplot as plt

try:
    img = cv2.imread(r'new_webcam_image.jpg',0)
    template = cv2.imread(r'table_template.jpg',0)
except IOError as e:
    print("({})".format(e))
else:
    img2 = img.copy()
    w, h = template.shape[::-1]

# All the 6 methods for comparison in a list
 methods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR',
             'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED']


for meth in methods:
    img = img2.copy()
    method = eval(meth)

    # Apply template Matching
    res = cv2.matchTemplate(img,template,method)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
    print("Method: %s" , meth)
    print("min_val: " , min_val)
    print("max_val: " , max_val)
    print("min_loc: " , min_loc)
    print("max_loc: " , max_loc)
    print(" ")
    # If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum
    if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
        top_left = min_loc
    else:
        top_left = max_loc
    bottom_right = (top_left[0] + w, top_left[1] + h)

    cv2.rectangle(img,top_left, bottom_right, 255, 2)

    plt.subplot(121),plt.imshow(res,cmap = 'gray')
    plt.title('Matching Result'), plt.xticks([]), plt.yticks([])
    plt.subplot(122),plt.imshow(img,cmap = 'gray')
    plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
    plt.suptitle(meth) #; plt.legend([min_val, max_val, min_loc, max_loc], ["min_val", "max_val", "min_loc", "max_loc"])

    plt.show()
    box = img[top_left[1]:top_left[1]+h,0:bottom_right[1]+w]
    cv2.imshow("cropped", box)
    cv2.waitKey(0)

我没有一个完整的解决方案给你。显示的代码基于我用来修复扫描仪输出的一些代码。对我来说,模板解决方案听起来是一种更好的方法,但是下面应该给你一些其他的方法。

import cv2

imageSrc = cv2.imread("test12.jpg")

# First cut the source down slightly
h = imageSrc.shape[0]
w = imageSrc.shape[1]
cropInitial = 50
imageSrc = imageSrc[100:50+(h-cropInitial*2), 50:50+(w-cropInitial*2)]

# Threshold the image and find edges (to reduce the amount of pixels to count)
ret, imageDest = cv2.threshold(imageSrc, 220, 255, cv2.THRESH_BINARY_INV)
imageDest = cv2.Canny(imageDest, 100, 100, 3)

# Create a list of remaining pixels
points = cv2.findNonZero(imageDest)

# Calculate a bounding rectangle for these points
hull = cv2.convexHull(points)
x,y,w,h = cv2.boundingRect(hull)

# Crop the original image to the bounding rectangle
imageResult = imageSrc[y:y+h,x:x+w]
cv2.imwrite("test12 cropped.jpg", imageResult)

产量没有你需要的多。使用各种阈值参数应该可以提高结果。

我建议在imageThreshimageDest上的不同点使用imshow,这样您就可以看到代码的每个阶段都发生了什么。希望这能帮助你进步。

相关问题 更多 >