问题描述
我有一堆图像,希望通过去除黑色边框来使图像均匀化.通常,我将Imagemagick的Trim函数与fuzz参数一起使用,但是如果图像带有水印,则结果不在此处.
I have a bunch of image I would like to uniformise by removing black borders. Usually I use the Trim function of Imagemagick with the fuzz parameters but in the case the image have some watermark the result is not here.
实际上,我正在使用opencv和形态学转换进行一些测试,以尝试识别水印和图像,然后选择更大的元素,但是我对opencv真的很陌生,我很挣扎.
Actually I'm making some tests with opencv and morphological transform to try to identify watermark and image and then select the bigger element but I'm really new with opencv and I struggle.
从左下角到右上角,水印无处不在.
Watermark can be everywhere, from bottom left to upper right.
我更喜欢Python代码,但是欢迎使用诸如Imagemagick之类的应用程序.
I would prefer a Python code but using some app like Imagemagick or similar is welcome.
实际上仅使用opencv我得到以下结果:
Actually using opencv only I get this result:
import copy
import cv2
from matplotlib import pyplot as plt
IMG_IN = '/data/black_borders/island.jpg'
# keep a copy of original image
original = cv2.imread(IMG_IN)
# Read the image, convert it into grayscale, and make in binary image for threshold value of 1.
img = cv2.imread(IMG_IN,0)
# use binary threshold, all pixel that are beyond 3 are made white
_, thresh_original = cv2.threshold(img, 3, 255, cv2.THRESH_BINARY)
# Now find contours in it.
thresh = copy.copy(thresh_original)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
# get contours with highest height
lst_contours = []
for cnt in contours:
ctr = cv2.boundingRect(cnt)
lst_contours.append(ctr)
x,y,w,h = sorted(lst_contours, key=lambda coef: coef[3])[-1]
# draw contours
ctr = copy.copy(original)
cv2.rectangle(ctr, (x,y),(x+w,y+h),(0,255,0),2)
# display results with matplotlib
# original
original = original[:,:,::-1] # flip color for maptolib display
plt.subplot(221), plt.imshow(original)
plt.title('Original Image'), plt.xticks([]),plt.yticks([])
# Threshold
plt.subplot(222), plt.imshow(thresh_original, cmap='gray')
plt.title('threshold binary'), plt.xticks([]),plt.yticks([])
# selected area for future crop
ctr = ctr[:,:,::-1] # flip color for maptolib display
plt.subplot(223), plt.imshow(ctr)
plt.title('Selected area'), plt.xticks([]),plt.yticks([])
plt.show()
结果:
推荐答案
要删除黑色边框:-
请点击此链接(我认为是完美答案):-
使用OpenCV裁剪黑色边缘
要通过指定区域删除黑色边框,请点击此链接
如何使用Python在OpenCV中裁剪图像
除了可以裁剪图像中的任何部分,您还可以仅使用ROI(感兴趣区域).为此,请点击以下链接,
如何在python中使用opencv复制图像区域? /a>
To remove black borders:-
Follow this link(Perfect Answer I think) :-
Crop black edges with OpenCV
To remove black border by specifying region, follow this link
How to crop an image in OpenCV using Python
Instead of cropping any part from image, you may take only ROI (Region of Interest). To do this, follow this link,
How to copy a image region using opencv in python?
要删除水印:-
如果水印可能出现在图像中的任何位置,则无法完全清除水印.只是您可以在该图像上应用模糊效果.它将模糊您的水印.
它的链接:
https://opencv-python-tutroals.readthedocs .org/zh-CN/latest/py_tutorials/py_imgproc/py_filtering/py_filtering.html
如果仅在黑色边框上存在水印,则上述方法将解决您的问题.
To remove watermark:-
If watermark may appear anywhere in your image means, you cannot clear watermark fully. Just you may apply blurring effect on that image. It will blur your watermark.
Its link :
https://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_imgproc/py_filtering/py_filtering.html
If watermark will exist only on the black border means, the above mentioned methods will solve your problem.
这篇关于在Python中删除带有水印的图像上的黑色边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!