如何从图像中删除黑色部分? [英] How to remove black part from the image?

查看:277
本文介绍了如何从图像中删除黑色部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用OpenCV函数和C ++将两个图像拼接在一起。现在我面临一个问题,最终的图像包含一个大的黑色部分。



最终图片应为包含有效部分的矩形。
我的形象如下:





如何删除黑色部分?

解决方案<

mevatron 的答案是黑色区域数量最小化,同时保留完整图像的一种方式。



另一个选项是删除完整的黑色区域,你也松动了一些部分的图像,但结果将是一个整洁的矩形图像。下面是Python代码。



这里,你会发现图像的三个主要角落如下:



img src =http://i.stack.imgur.com/KtqOg.jpgalt =enter image description here>



我已标记这些值。 (1,x2),(x1,1),(x3,y3)。它是基于您的图片从(1,1)开始的假设。



代码:



第一步与 mevatron 相同。模糊图片以消除噪点,设定图片的起点,然后找出轮廓。

  import cv2 
import numpy as np

img = cv2.imread('office.jpg')
img = cv2.resize(img,(800,400))

gray = cv2.cvtColor ,cv2.COLOR_BGR2GRAY)
gray = cv2.medianBlur(gray,3)

ret,thresh = cv2.threshold(grey,1,255,0)
轮廓,等级= cv2 .findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

现在找到最大的轮廓图片。它是为了避免噪声的情况下如果有(很可能不会有任何)。或者您可以使用 mevatron 的方法。

  max_area = -1 
best_cnt = None

for cnt in contour:

area = cv2.contourArea(cnt)
如果area> max_area:
max_area = area
best_cnt = cnt

删除所找到的轮廓值中的不必要的点,但保留所有角值。

  approx = cv2.approxPolyDP(best_cnt,0.01 * cv2 .arcLength(best_cnt,True),True)

现在我们找到了拐角。



首先,我们找到(x3,y3)。这是最远的点。因此, x3 * y3 将非常大。因此,我们找到所有点对的产品,并选择具有最大乘积的对。

  far = approx [np.product ,2).argmax()] [0] 

  ymax = approx [approx [:,:, 0] == 1] .max()

  xmax = approx [approx [:,:, 1] == 1] .max()

现在我们发现 (far.x,xmax)和(far.y,ymax)

  x = min(far [0],xmax)
y = min(far [1],ymax)

如果你用(1,1)和(x,y)绘制一个矩形,你得到如下结果:





因此,您可裁剪图片以修正矩形区域。

  img2 = img [:y,:x] .copy()

以下是结果:





看到,问题是你丢失了一些部分的拼接图像。 / code>


I have stitched two images together using OpenCV functions and C++. Now I am facing a problem that the final image contains a large black part.

The final image should be a rectangle containing the effective part. My image is the following:

How can I remove the black section?

解决方案

mevatron's answer is one way where amount of black region is minimised while retaining full image.

Another option is removing complete black region where you also loose some part of image, but result will be a neat looking rectangular image. Below is the Python code.

Here, you find three main corners of the image as below:

I have marked those values. (1,x2), (x1,1), (x3,y3). It is based on the assumption that your image starts from (1,1).

Code :

First steps are same as mevatron's. Blur the image to remove noise, threshold the image, then find contours.

import cv2
import numpy as np

img = cv2.imread('office.jpg')
img = cv2.resize(img,(800,400))

gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
gray = cv2.medianBlur(gray,3)

ret,thresh = cv2.threshold(gray,1,255,0)
contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

Now find the biggest contour which is your image. It is to avoid noise in case if any (Most probably there won't be any). Or you can use mevatron's method.

max_area = -1
best_cnt = None

for cnt in contours:

    area = cv2.contourArea(cnt)
    if area > max_area:
        max_area = area
        best_cnt = cnt

Now approximate the contour to remove unnecessary points in contour values found, but it preserve all corner values.

approx = cv2.approxPolyDP(best_cnt,0.01*cv2.arcLength(best_cnt,True),True)

Now we find the corners.

First, we find (x3,y3). It is farthest point. So x3*y3 will be very large. So we find products of all pair of points and select the pair with maximum product.

far = approx[np.product(approx,2).argmax()][0]

Next (1,x2). It is the point where first element is one,then second element is maximum.

ymax = approx[approx[:,:,0]==1].max()

Next (x1,1). It is the point where second element is 1, then first element is maximum.

xmax = approx[approx[:,:,1]==1].max()

Now we find the minimum values in (far.x,xmax) and (far.y, ymax)

x = min(far[0],xmax)
y = min(far[1],ymax)

If you draw a rectangle with (1,1) and (x,y), you get result as below:

So you crop the image to correct rectangular area.

img2 = img[:y,:x].copy()

Below is the result:

See, the problem is that you lose some parts of the stitched image.

这篇关于如何从图像中删除黑色部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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