This action will force synchronization from 小柒2012商城/从零学Python, which will overwrite any changes that you have made since you forked the repository, and can not be recovered!!!
Synchronous operation will process in the background and will refresh the page when finishing processing. Please be patient.
# -*- coding:utf-8 -*-from imutils.perspective import four_point_transformfrom imutils import contoursimport numpy as npimport cv2 as cv# 加载原图,可在项目imgs/example02目录下找到img = cv.imread("test01.jpg")# cv.resizeWindow("enhanced", 240, 280);# 打印原图cv.imshow("orgin", img)# 灰度化gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)# 打印灰度图cv.imshow("gray", gray)# 高斯滤波,清除一些杂点blur = cv.GaussianBlur(gray, (3, 3), 0)# 自适应二值化算法thresh2 = cv.adaptiveThreshold(blur, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY_INV, 131, 4)# 打印二值化后的图cv.imshow("thresh2", thresh2)# 寻找轮廓image, cts, hierarchy = cv.findContours(thresh2, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)# 打印找到的轮廓print("轮廓数:", len(cts))# 对拷贝的原图进行轮廓标记contour_flagged = cv.drawContours(img.copy(), cts, -1, (0, 0, 255), 3)# 打印轮廓图cv.imshow("contours_flagged", contour_flagged)# 按像素面积降序排序list = sorted(cts, key=cv.contourArea, reverse=True)# 遍历轮廓for ct in list:# 周长,第1个参数是轮廓,第二个参数代表是否是闭环的图形peri = 0.01 * cv.arcLength(ct, True)# 获取多边形的所有定点,如果是四个定点,就代表是矩形approx = cv.approxPolyDP(ct, peri, True)# 只考虑矩形if len(approx) == 4:# 从原图中提取所需的矫正图片ox = four_point_transform(img, approx.reshape(4, 2))# 从原图中提取所需的矫正图片tx = four_point_transform(gray, approx.reshape(4, 2))# 打印矫正后的灰度图cv.imshow("tx", tx)# 对矫正图进行高斯模糊blur = cv.GaussianBlur(tx, (3, 3), 0)# 对矫正图做自适应二值化thresh2 = cv.adaptiveThreshold(blur, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY_INV, 131, 4)# 打印矫正后的二值化图cv.imshow("tx_thresh2", thresh2)# 获取轮廓r_image, r_cts, r_hierarchy = cv.findContours(thresh2, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)# 打印得到轮廓数量print("第二层轮廓数:", len(r_cts))# 用于存储答案的python list变量question_list = []for r_ct in r_cts:# 转为矩形,分别获取 x,y坐标,及矩形的宽和高x, y, w, h = cv.boundingRect(r_ct)# 过滤掉不符合答案坐标和长宽的选项if x > 2 and y > 2 and w > 20 and h > 20:# cv.drawContours(ox, r_ct, -1, (0, 0, 255), 1)question_list.append(r_ct)print("答案总数:", len(question_list))# 按坐标从上到下排序questionCnts = contours.sort_contours(question_list, method="top-to-bottom")[0]# 使用np函数,按5个元素,生成一个集合for (q, i) in enumerate(np.arange(0, len(questionCnts), 5)):# 每一个行5个答案,从左到右排序cnts = contours.sort_contours(questionCnts[i:i + 5])[0]# 存储一行题里面的每个答案ans_list = []for (j, cc) in enumerate(cnts):# 生成全黑画布mask = np.zeros(thresh2.shape, dtype="uint8")# 将每一个答案按轮廓写上去,并将填充颜色设置成白色tpp = cv.drawContours(mask, [cc], -1, 255, -1)# 两个图片做位运算mask = cv.bitwise_and(thresh2, thresh2, mask=mask)# 统计每个答案的像素total = cv.countNonZero(mask)# 添加到集合里面ans_list.append((total, j))# 按像素大小排序ans_list = sorted(ans_list, key=lambda x: x[0], reverse=True)max_ans_num = ans_list[0][1]max_ans_size = ans_list[0][0]print("答案序号:", max_ans_num, "列表:", ans_list)# 给选中答案,标记成红色cv.drawContours(ox, cnts[max_ans_num], -1, (0, 0, 255), 2)cv.imshow("answer_flagged", ox)# 最大的轮廓就是我们想要的,之后的就可以结束循环了break# 阻塞等待窗体关闭cv.waitKey(0)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。