如何在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]裁剪我的图像。

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

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

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,这样您就可以看到代码中每个阶段发生的情况。

相关内容

  • 没有找到相关文章

最新更新