如何解决Python3 skimage - 计算图片数量
我有一个图像,我正在使用 skimage 尝试检测:
-
页面中实际有多少张图片 - 我希望它“计数”5
-
找到每个图像的角 - 所以如果它比我们的 maxCorners 数 5 多 5*4=20
-
在每个角之间绘制直线并“遮罩”5 个图像中的每一个
现在我得到的只是正在读取的图像,填充漏洞,仅此而已 - 其他方面的指导?
from scipy import ndimage as nd
import imageio
from skimage import io,filters
from scipy import ndimage
import matplotlib.pyplot as plt
filename = "C:\\Users\\Tony\\Pictures\\img807.tif"
im = io.imread(filename,as_gray=True)
val = filters.threshold_otsu(im)
drops = ndimage.binary_fill_holes(im < val)
plt.imshow(drops,cmap='gray')
plt.show()
我试过查看这些资源:
特别是最后一个角点检测...
真实来源链接(12 小时):https://u.pcloud.link/publink/show?code=XZDONYXZVkUNFT2qcEFk4nFYYnx7d8swzaD7
解决方法
这个Answer是解决这个问题的关键。
坐标:
[[(38,11),(251,364)],[(254,62),(592,266)],312),
(592,518)],[(46,456),(247,797)],[(346,557),(526,797)]]
import numpy as np
import matplotlib.pyplot as plt
import cv2
import itertools
#====================================================
img = cv2.imread('input.jpg',0)
blur = cv2.blur(img,(3,3))
blur[blur>225] = 0
sobelx = cv2.Sobel(blur,cv2.CV_64F,1,ksize=5)
sobely = cv2.Sobel(blur,ksize=5)
sobel = np.sqrt( sobelx**2 + sobely**2)
sobel = (255 * sobel)/(sobel.max() - sobel.min())
sobel = sobel.astype(np.uint8)
sobel[sobel<20] = 0
sobel[sobel>20] = 255
#====================================================
_,thresh = cv2.threshold(blur,127,255,1)
thresh = thresh + sobel
median = cv2.medianBlur(thresh,3)
gray_scale = median.copy()
image = np.stack([img,img,img],axis=2)
img_bin = cv2.Canny(gray_scale,50,110)
dil_kernel = np.ones((3,3),np.uint8)
img_bin=cv2.dilate(img_bin,dil_kernel,iterations=1)
line_min_width = 7
kernal_h = np.ones((2,line_min_width),np.uint8)
img_bin_h = cv2.morphologyEx(img_bin,cv2.MORPH_OPEN,kernal_h)
kernal_v = np.ones((line_min_width,1),np.uint8)
img_bin_v = cv2.morphologyEx(img_bin,kernal_v)
img_bin_final=img_bin_h|img_bin_v
final_kernel = np.ones((3,np.uint8)
img_bin_final=cv2.dilate(img_bin_final,final_kernel,iterations=1)
_,_,stats,_ = cv2.connectedComponentsWithStats(~img_bin_final,connectivity=8,ltype=cv2.CV_32S)
coords = []
### 1 and 0 and the background and residue connected components whihc we do not require
for x,y,w,h,area in stats[2:]:
if area>15000:
coords.append([(x,y),(x+w,y+h)])
def bb_intersection(coords,boxA,boxB):
# determine the (x,y)-coordinates of the intersection rectangle
xA = max(boxA[0][0],boxB[0][0])
yA = max(boxA[0][1],boxB[0][1])
xB = min(boxA[1][0],boxB[1][0])
yB = min(boxA[1][1],boxB[1][1])
# compute the area of intersection rectangle
interArea = max(0,xB - xA + 1) * max(0,yB - yA + 1)
# compute the area of both rectangles
boxAArea = (boxA[1][0] - boxA[0][0] + 1) * (boxA[1][1] - boxA[0][1] + 1)
boxBArea = (boxB[1][0] - boxB[0][0] + 1) * (boxB[1][1] - boxB[0][1] + 1)
if(interArea == boxAArea):
coords.remove(boxA)
elif(interArea == boxBArea):
coords.remove(boxB)
#
for boxa,boxb in itertools.combinations(coords,2):
bb_intersection(coords,boxa,boxb)
for coord in coords:
cv2.rectangle(image,coord[0],coord[1],(0,0),1)
print(coords)
plt.imshow(image)
plt.title("There are {} images".format(len(coords)))
plt.axis('off')
plt.show()
编辑:
这个答案不是通用的解决方案,必须相应地调整参数,对于原始图像更改此块,代码将完美运行:
img = cv2.imread('input.tif',0)
img = cv2.resize(img,(605,830))
blur = img.copy()
blur[blur>225] = 0
sobelx = cv2.Sobel(blur,ksize=3)
sobely = cv2.Sobel(blur,ksize=3)
sobel = np.sqrt( sobelx**2 + sobely**2)
sobel = (255 * sobel)/(sobel.max() - sobel.min())
sobel = sobel.astype(np.uint8)
sobel[sobel<40] = 0
sobel[sobel>40] = 255
坐标:
[[(31,16),(240,[(253,56),(600,265)],309),
(605,520)],[(40,(248,803)],[(347,(534,803)]]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。