门户网站php源码,黄埔网站建设优化seo,整人关不掉的网站怎么做,wordpress 视频主题本节观点#xff1a;一个好的提问就已经解决了问题的90%。
对于问题的描述正确与否决定了解决问题的方法和路径#xff0c;所以我们在AI时代必须要学会正确的描述问题和表达问题#xff0c;否则即使有AI辅助也是很难精准的解决问题。
我的问题#xff1a;
如何利用代码从图…本节观点一个好的提问就已经解决了问题的90%。
对于问题的描述正确与否决定了解决问题的方法和路径所以我们在AI时代必须要学会正确的描述问题和表达问题否则即使有AI辅助也是很难精准的解决问题。
我的问题
如何利用代码从图片集中筛选出出血的图片定义出血提取图片中的边缘我希望通过设置一个值来界定比如所有边缘线与图像边缘接触的总条数超过10条才算出血图片另外图像中的边缘线段至少长20像素才计入有效边缘线段同时要考虑图片路径含有中文的问题。
如下图所示其中12是出血的图34是没有出血的图。通过上方的描述提交给AIAI就能帮我们实现批量筛选所有出血的图片。特别说明
国产AI水平确实远低于国外的同样的问题GPT一次就搞定了代码而豆包qwendeepseek等从多国产AI提问了N轮最后连最基本的读图时中文路径问题都无法搞定可笑至极。加油国产AI。
import cv2
import numpy as np
import os
import shutildef read_image_chinese_path(image_path):# 支持中文路径读取image_data np.fromfile(image_path, dtypenp.uint8)img cv2.imdecode(image_data, cv2.IMREAD_COLOR)return imgdef is_bleeding_image(image_path, edge_touch_threshold10, min_edge_length20):img read_image_chinese_path(image_path)if img is None:print(f无法读取图片: {image_path})return Falseheight, width img.shape[:2]# 灰度 Canny 边缘检测gray cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)edges cv2.Canny(gray, 100, 200)# 查找轮廓contours, _ cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)edge_touch_count 0for cnt in contours:# 计算轮廓长度length cv2.arcLength(cnt, closedFalse)if length min_edge_length:continue # 过滤掉短轮廓# 检查是否有点在图像边界for point in cnt:x, y point[0]if x 0 or y 0 or x width-1 or y height-1:edge_touch_count 1break # 这条轮廓计数一次就够return edge_touch_count edge_touch_thresholddef find_and_move_bleeding_images(folder_path, output_folder, edge_touch_threshold10, min_edge_length20):if not os.path.exists(output_folder):os.makedirs(output_folder)for root, _, files in os.walk(folder_path):for file in files:if file.lower().endswith((.png, .jpg, .jpeg, .bmp)):file_path os.path.join(root, file)if is_bleeding_image(file_path, edge_touch_threshold, min_edge_length):print(f检测到出血: {file_path})# 生成输出路径output_path os.path.join(output_folder, file)# 如果重名文件改名避免覆盖base, ext os.path.splitext(output_path)counter 1while os.path.exists(output_path):output_path f{base}_{counter}{ext}counter 1# 移动文件shutil.move(file_path, output_path)if __name__ __main__:input_folder rimages # 输入图片文件夹路径output_folder routput_images # 检测到出血后保存到这里find_and_move_bleeding_images(input_folder, output_folder,edge_touch_threshold10,min_edge_length20)print(✅ 所有出血图片已移动至 output_images 文件夹)
最后描述问题过程
第1次
我有几张图片其中有一张图片内容是非常完整的主体在图的中间这里要考虑图片的背景有的图片背景是纯白色有的图片是纯黑色有的图片的是其它纯色的背景而其余的图片都是主体出血。如何利用代码检测或筛选出主体没出血的图片
出来的代码只能做到60%选对。
第2次
重新整理一下思路1分别取出图像外边1像素宽的边缘2.分别计算每边的像素色值是否一样如果计算出有明显不同的像素色值部分的像素总计超过5%单边所有像素色值比例的说明是出血图像。
出来的代码只能做到65%选对。
第3次
如何利用代码从图片集中筛选出出血的图片定义出血提取图片中的边缘我希望通过设置一个值来界定比如所有边缘线与图像边缘接触的总条数超过10条才算出血图片另外图像中的边缘线段至少长20像素才计入有效边缘线段同时要考虑图片路径含有中文的问题。
出来的代码只能做到95%选对。
最终代码
import cv2
import numpy as np
import os
import shutildef read_image_chinese_path(image_path):支持中文路径读取image_data np.fromfile(image_path, dtypenp.uint8)img cv2.imdecode(image_data, cv2.IMREAD_COLOR)return imgdef has_large_colored_block_with_edges(img, block_size10, edge_proximity5):检查是否存在大于 block_size 的色块且色块两侧有边缘线并且色块接近图像边缘height, width img.shape[:2]# 边缘检测gray cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)edges cv2.Canny(gray, 100, 200)# 查找轮廓contours, _ cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)for cnt in contours:x, y, w, h cv2.boundingRect(cnt)# 判断色块尺寸是否足够大if w block_size and h block_size:# 检查色块是否靠近图像边缘if (x edge_proximity or y edge_proximity orx w width - edge_proximity ory h height - edge_proximity):# 检查 ROI 内边缘线roi_edges edges[y:yh, x:xw]top_edge np.sum(roi_edges[0, :] 0)bottom_edge np.sum(roi_edges[-1, :] 0)left_edge np.sum(roi_edges[:, 0] 0)right_edge np.sum(roi_edges[:, -1] 0)edge_threshold 3 # 至少3个像素视作边缘线if (left_edge edge_threshold and right_edge edge_threshold):return True # 找到了符合要求的色块return Falsedef is_bleeding_image(image_path, edge_touch_threshold10, min_edge_length20, block_size10):img read_image_chinese_path(image_path)if img is None:print(f无法读取图片: {image_path})return Falseheight, width img.shape[:2]# 灰度 边缘检测gray cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)edges cv2.Canny(gray, 100, 200)# 查找所有外轮廓contours, _ cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)edge_touch_count 0for cnt in contours:# 计算轮廓长度length cv2.arcLength(cnt, closedFalse)if length min_edge_length:continue# 检查是否触碰图像边界for point in cnt:x, y point[0]if x 0 or y 0 or x width-1 or y height-1:edge_touch_count 1break# 条件1足够多的触边线条if edge_touch_count edge_touch_threshold:return False# 条件2是否有色块两侧有边缘if not has_large_colored_block_with_edges(img, block_size):return Falsereturn True # 两个条件都满足def find_and_move_bleeding_images(folder_path, output_folder, edge_touch_threshold10, min_edge_length20, block_size10):if not os.path.exists(output_folder):os.makedirs(output_folder)for root, _, files in os.walk(folder_path):for file in files:if file.lower().endswith((.png, .jpg, .jpeg, .bmp)):file_path os.path.join(root, file)if is_bleeding_image(file_path, edge_touch_threshold, min_edge_length, block_size):print(f检测到出血: {file_path})# 生成输出路径output_path os.path.join(output_folder, file)base, ext os.path.splitext(output_path)counter 1while os.path.exists(output_path):output_path f{base}_{counter}{ext}counter 1# 移动文件shutil.move(file_path, output_path)if __name__ __main__:input_folder rimages # 输入文件夹路径output_folder routput_images # 出血图片保存路径find_and_move_bleeding_images(input_folder, output_folder,edge_touch_threshold1, # 触边线条数阈值min_edge_length2, # 有效边缘线长度block_size2 # 色块尺寸阈值)print(✅ 所有出血图片已移动至 output_images 文件夹)