作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
首先,很难解释。如果某人的标题更好,请随时进行编辑/建议。
因此,我正在使用下一个代码从给定图像中删除ROI。
import cv2
import os
import numpy as np
import shutil
src = (os.path.expanduser('~\\Desktop\\output\\'))
causali = os.listdir(src) # CREO LISTA CAUSALI-2
causali.sort(key=lambda x: int(x.split('.')[0]))
for file in enumerate(causali): # CONTA NUMERO DI FILE CAUSALE
#import image
image = cv2.imread(os.path.expanduser('~\\Desktop\\output\\{}'.format(file[1])))
cv2.imshow('orig',image)
cv2.waitKey(0)
#grayscale
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
#cv2.imshow('gray',gray)
#cv2.waitKey(0)
#binary
ret,thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY_INV)
#cv2.imshow('second',thresh)
#cv2.waitKey(0)
#dilation
kernel = np.ones((1,80), np.uint8)
img_dilation = cv2.dilate(thresh, kernel, iterations=1)
#cv2.imshow('dilated',img_dilation)
#cv2.waitKey(0)
#find contours
im2,ctrs, hier = cv2.findContours(img_dilation.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
#sort contours
sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0])
for i, ctr in enumerate(sorted_ctrs):
# Get bounding box
x, y, w, h = cv2.boundingRect(ctr)
# Getting ROI
roi = image[y:y+h, x:x+w]
if h < 25:
clean = cv2.rectangle(image,(x,y),( x + w, y + h ),(255,255,255),-1)
cv2.imwrite(os.path.expanduser('~\\Desktop\\output2\\{}.png').format(file[0]), clean)
h < 25
来删除我不想在最终图像中可见的ROI。
最佳答案
我重写代码,在每次处理之前进行复制,并用颜色填充它们,现在更加清楚:
import cv2
import os
import numpy as np
causali = os.listdir("causali")
causali.sort(key=lambda x: int(x.split('.')[0]))
print(causali)
for idx, fname in enumerate(causali):
fname = os.path.expanduser("causali/"+fname)
print(fname)
img = cv2.imread(fname)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY_INV)
kernel = np.ones((1,80), np.uint8)
dilated = cv2.dilate(thresh, kernel, iterations=1)
cnts = cv2.findContours(dilated.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
cnts = sorted(cnts, key=lambda cnt: cv2.boundingRect(cnt)[0])
## make an copy first
clean = img.copy()
for i, cnt in enumerate(cnts):
x, y, w, h = cv2.boundingRect(cnt)
roi = img[y:y+h, x:x+w]
if h < 25:
#clean = cv2.rectangle(img,(x,y),( x + w, y + h ),(255,255,255),-1)
clean = cv2.rectangle(img,(x,y),( x + w, y + h ),(0,255,0),-1)
## save the "clean"
cv2.imwrite(os.path.expanduser("output/{}.png").format(idx), clean)
关于python - 文件被无故复制(Python,OpenCV),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47782595/
我有一个小型 Java 11 示例,其中包含 JUnit 5 测试,结果如下: changed conditional boundary → SURVIVED 主类: public final cla
我是一名优秀的程序员,十分优秀!