南阳开网站制作,教务系统网站怎么做,wordpress音频播放不了,dw里面怎么做网站轮播图用户常常将多张图拼成一张图。
如果将这张图拆为多个子图#xff0c;下面是一种opencv的办法#xff0c;后面要训练一个模型来识别边缘更为准确。
import osimport cv2
import numpy as npdef detect_lines(image_path):# 读取图片image cv2.imread(image_path)if image i…用户常常将多张图拼成一张图。
如果将这张图拆为多个子图下面是一种opencv的办法后面要训练一个模型来识别边缘更为准确。
import osimport cv2
import numpy as npdef detect_lines(image_path):# 读取图片image cv2.imread(image_path)if image is None:raise ValueError(无法读取图片请检查路径是否正确)# 将图片转为灰度图gray cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)# 使用Canny边缘检测edges cv2.Canny(gray, 20, 240, apertureSize3)# 使用霍夫变换检测线段lines cv2.HoughLinesP(edges, 1, np.pi / 180, threshold100, minLineLength300, maxLineGap10)chuizhi []shuiping []# 筛选出水平和垂直的线段并绘制if lines is not None:for line in lines:for x1, y1, x2, y2 in line:if abs(y1 - y2) 5: # 水平线段shuiping.append((x1, y1, x2, y2))elif abs(x1 - x2) 5:chuizhi.append((x1, y1, x2, y2))if len(shuiping) 0 and len(chuizhi) 0:return [image]# 拆图ys []for x1, y1, x2, y2 in shuiping:ys.append(y1)ys.append(y2)ys.sort()ys [0] ys [image.shape[0]]y_images []for i in range(len(ys) - 1):if ys[i 1] - ys[i] 100:continuey_images.append(image[ys[i]:ys[i 1], :])xs []for x1, y1, x2, y2 in chuizhi:xs.append(x1)xs.append(x2)xs.sort()xs [0] xs [image.shape[1]]x_images []for i in range(len(xs) - 1):if xs[i 1] - xs[i] 100:continuefor y_image in y_images:x_images.append(y_image[:, xs[i]:xs[i 1]])# 去除宽高比超过5的x_images [x_image for x_image in x_images ifx_image.shape[0] / x_image.shape[1] 5 or x_image.shape[1] / x_image.shape[0] 5]return x_imagesdef listPathAllfiles(dirname):result []for maindir, subdir, file_name_list in os.walk(dirname):for filename in file_name_list:apath os.path.join(maindir, filename)result.append(apath)return resultsrc rC:\Users\Administrator\Pictures\girl_no_train\mangguo
dst rC:\Users\Administrator\Pictures\girl_no_train\mangguo_dst
if not os.path.exists(dst):os.makedirs(dst)
files listPathAllfiles(src)
for file in files:x_images detect_lines(file)for i, x_image in enumerate(x_images):cv2.imwrite(f{dst}/{os.path.basename(file)}_{i}.jpg, x_image)