Opencv和python用于自动裁剪 [英] Opencv and python for auto cropping

查看:211
本文介绍了Opencv和python用于自动裁剪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自动裁剪图片。

我正在使用ImageMagick。

I am using ImageMagick for this .

命令我是使用

 convert  3.jpg  -fuzz 10%  -trim     trim.jpg

我如何解决这个问题。

我觉得模糊因素有问题这是设置。

I think there is problem with fuzz factor which am setting .

推荐答案

如果你想用OpenCV做这个,一个好的起点可能是在做了一些简单的处理之后删除图像中的噪点和细节,您可以找到图像的边缘,然后找到边界框并裁剪到该区域。但是在第二张图像的情况下,您可能需要进行一些后处理,因为原始边缘可能会保留一些噪点和边框。您可以逐个像素地执行此操作,或者另一个可能是矫枉过正的方法将查找图像中的所有轮廓并找到最大的边界框。使用此功能,您可以获得以下结果:

If you want to do this with OpenCV, a good starting point may be after doing some simple processing to remove noise and small details in the image, you can find the edges of the image and then find the bounding box and crop to that area. But in case of your second image, you may need to do some post-processing as the raw edges may hold some noise and borders. You can do this on a pixel-by-pixel basis, or another maybe overkill method would be finding all the contours in the image and the finding the biggest bounding box. Using this you can get the following results:

第二个:

需要工作的部分是找到适用于所有人的正确阈值处理方法图片。在这里,我使用不同的阈值来制作二进制图像,因为第一个主要是白色,第二个是有点暗。第一个猜测是使用平均强度作为线索。

The part that needs work is finding a proper thresholding method that works for all the images. Here I used different thresholds to make a binary image, as the first one was mostly white and second one was a bit darker. A first guess would be using the average intensity as a clue.

希望这有帮助!

编辑

这是我使用一些预处理和动态阈值来使其适用于两个图像的方式:

This is how I used some pre-processing and also a dynamic threshold to get it work for both of the images:

im = cv2.imread('cloth.jpg')
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
imgray = cv2.blur(imgray,(15,15))
ret,thresh = cv2.threshold(imgray,math.floor(numpy.average(imgray)),255,cv2.THRESH_BINARY_INV)
dilated=cv2.morphologyEx(thresh, cv2.MORPH_OPEN, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(10,10)))
_,contours,_ = cv2.findContours(dilated,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

我还检查了轮廓区域以去除非常大的轮廓:

I also checked the contour area to remove very large contours:

new_contours=[]
for c in contours:
    if cv2.contourArea(c)<4000000:
        new_contours.append(c)

数字 400 0000 是对图像尺寸(宽度*高度)的估计,大轮廓应该有一个接近图像尺寸的区域。

The number 4000000 is an estimation of the image size (width*height), big contours should have an area close to the image size.

然后你可以迭代所有轮廓,并找到整个边界框:

Then you can iterate all the contours, and find the overall bounding box:

best_box=[-1,-1,-1,-1]
for c in new_contours:
   x,y,w,h = cv2.boundingRect(c)
   if best_box[0] < 0:
       best_box=[x,y,x+w,y+h]
   else:
       if x<best_box[0]:
           best_box[0]=x
       if y<best_box[1]:
           best_box[1]=y
       if x+w>best_box[2]:
           best_box[2]=x+w
       if y+h>best_box[3]:
           best_box[3]=y+h

然后你有 best_box 数组中所有轮廓的边界框。

Then you have the bounding box of all contours inside the best_box array.

这是结果第三张图片:

Here is the result for the third image:

这篇关于Opencv和python用于自动裁剪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆