]来自https://www.pyimagesearch.com/2018/07/19/opencv-tutorial-a-guide-to-learn-opencv/
我能提取轮廓并写成文件。例如,我有一张照片,上面有一些潦草的文字:"in there“。
我已经能够提取字母作为单独的文件,但我想要的是,这些字母文件应该有相同的宽度和高度。例如,在"i“和"r”宽度不同的情况下。在这种情况下,我想将(任何b/w像素)附加到"i“图片的右边,使其宽度与"r”的宽度相同。
怎么用Python来做呢?只需增加照片的大小(而不是调整大小)
我的代码如下所示:
# find contours (i.e., outlines) of the foreground objects in the
# thresholded image
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
output = image.copy()
ROI_number = 0
for c in cnts:
x,y,w,h = cv2.boundingRect(c)
ROI = image[y:y+h, x:x+w]
file = 'ROI_{}.png'.format(ROI_number)
cv2.imwrite(file.format(ROI_number), ROI)
[
][1
发布于 2020-02-08 01:15:08
下面是几种其他方法,使用Python/OpenCV,使用cv2.以便将边框向右扩展50个像素。第一种方法只是通过复制扩展边框。第二种方法是使用平均(平均)蓝色背景颜色,使用一个掩码来只获取蓝色像素。
输入:
import cv2
import numpy as np
# read image
img = cv2.imread('i.png')
# get mask of background pixels (for result2b only)
lowcolor = (232,221,163)
highcolor = (252,241,183)
mask = cv2.inRange(img, lowcolor, highcolor)
# get average color of background using mask on img (for result2b only)
mean = cv2.mean(img, mask)[0:3]
color = (mean[0],mean[1],mean[2])
# extend image to the right by 50 pixels
result = img.copy()
result2a = cv2.copyMakeBorder(result, 0,0,0,50, cv2.BORDER_REPLICATE)
result2b = cv2.copyMakeBorder(result, 0,0,0,50, cv2.BORDER_CONSTANT, value=color)
# view result
cv2.imshow("img", img)
cv2.imshow("mask", mask)
cv2.imshow("result2a", result2a)
cv2.imshow("result2b", result2b)
cv2.waitKey(0)
cv2.destroyAllWindows()
# save result
cv2.imwrite("i_extended2a.jpg", result2a)
cv2.imwrite("i_extended2b.jpg", result2b)
复制结果:
平均背景色结果:
发布于 2020-02-07 21:17:00
在Python/OpenCV/Numpy中,您可以创建一个大小和背景色的新图像。然后使用numpy切片将旧图像插入到新图像中。例如:
输入:
import cv2
import numpy as np
# read image
img = cv2.imread('i.png')
ht, wd, cc= img.shape
# create new image of desired size (extended by 50 pixels in width) and desired color
ww = wd+50
hh = ht
color = (242,231,173)
result = np.full((hh,ww,cc), color, dtype=np.uint8)
# copy img image into image at offsets yy=0,xx=0
yy=0
xx=0
result[yy:yy+ht, xx:xx+wd] = img
# view result
cv2.imshow("result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
# save result
cv2.imwrite("i_extended.jpg", result)
https://stackoverflow.com/questions/60119595
复制相似问题