效果图
代码:
# #day03 # # #将多张不同大小、通道的图片展示在同一窗口 # # 定义函数,第一个参数是缩放比例,第二个参数是需要显示的图片组成的元组或者列表 # def ManyImgs(scale, imgarray): # rows = len(imgarray) # 元组或者列表的长度 # cols = len(imgarray[0]) # 如果imgarray是列表,返回列表里第一幅图像的通道数,如果是元组,返回元组里包含的第一个列表的长度 # # print("rows=", rows, "cols=", cols) # # # 判断imgarray[0]的类型是否是list # # 是list,表明imgarray是一个元组,需要垂直显示 # rowsAvailable = isinstance(imgarray[0], list) # # # 第一张图片的宽高 # width = imgarray[0][0].shape[1] # height = imgarray[0][0].shape[0] # # print("width=", width, "height=", height) # # # 如果传入的是一个元组 # if rowsAvailable: # for x in range(0, rows): # for y in range(0, cols): # # 遍历元组,如果是第一幅图像,不做变换 # if imgarray[x][y].shape[:2] == imgarray[0][0].shape[:2]: # imgarray[x][y] = cv2.resize(imgarray[x][y], (0, 0), None, scale, scale) # # 将其他矩阵变换为与第一幅图像相同大小,缩放比例为scale # else: # imgarray[x][y] = cv2.resize(imgarray[x][y], (imgarray[0][0].shape[1], imgarray[0][0].shape[0]), None, scale, scale) # # 如果图像是灰度图,将其转换成彩色显示 # if len(imgarray[x][y].shape) == 2: # imgarray[x][y] = cv2.cvtColor(imgarray[x][y], cv2.COLOR_GRAY2BGR) # # # 创建一个空白画布,与第一张图片大小相同 # imgBlank = np.zeros((height, width, 3), np.uint8) # hor = [imgBlank] * rows # 与第一张图片大小相同,与元组包含列表数相同的水平空白图像 # for x in range(0, rows): # # 将元组里第x个列表水平排列 # hor[x] = np.hstack(imgarray[x]) # ver = np.vstack(hor) # 将不同列表垂直拼接 # # 如果传入的是一个列表 # else: # # 变换操作,与前面相同 # for x in range(0, rows): # if imgarray[x].shape[:2] == imgarray[0].shape[:2]: # imgarray[x] = cv2.resize(imgarray[x], (0, 0), None, scale, scale) # else: # imgarray[x] = cv2.resize(imgarray[x], (imgarray[0].shape[1], imgarray[0].shape[0]), None, scale, scale) # if len(imgarray[x].shape) == 2: # imgarray[x] = cv2.cvtColor(imgarray[x], cv2.COLOR_GRAY2BGR) # # 将列表水平排列 # hor = np.hstack(imgarray) # ver = hor # return ver # # #绘制轮廓 # def getContours(img): # coutours,hierarchy=cv2.findContours(img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) # for cnt in coutours: # area=cv2.contourArea(cnt) # if area>100: # cv2.drawContours(imgcontours, cnt, -1, (0, 0, 255), 5) # #周长 # peri=cv2.arcLength(cnt,True) # print(peri) # #拟合轮廓点集 # approx=cv2.approxPolyDP(cnt,0.03*peri,True) # print((approx)) # objcor=len(approx) # # x,y,w,h=cv2.boundingRect(approx) # cv2.rectangle(imgcontours,(x,y),(x+w,y+h),(255,0,0),5) # if objcor==3: # # imgr=paint_chinese_opencv(imgcontours,"三角形",(x+(w//2)-10,y+(h//2)-10),(255,255,255)) # cv2.putText(imgcontours,"SanJiaoXing",(x+(w//2)-10,y+(h//2)-10),cv2.FONT_HERSHEY_COMPLEX,1,(0,0,0),2) # elif objcor==4:cv2.putText(imgcontours,"ZhengFangXing",(x+(w//2)-10,y+(h//2)-10),cv2.FONT_HERSHEY_COMPLEX,1,(0,0,0),2) # else:cv2.putText(imgcontours,str(objcor)+"YuanXing",(x+(w//2)-10,y+(h//2)-10),cv2.FONT_HERSHEY_COMPLEX,1,(0,0,0),2) # # # img1=cv2.imread("C:/Users/31132/Desktop/xingz.jpg") # imgcontours=img1.copy() # imggry=cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY) # imgblur=cv2.GaussianBlur(imggry,(3,3),2) # imgcanny=cv2.Canny(imgblur,30,30) # # getContours(imgcanny) # imgblank=np.zeros_like(img1) # imgs=ManyImgs(0.3,([img1,imggry,imgblur],[imgcanny,imgcontours,imgblank])) # # cv2.imshow("1",imgs) # # imgsres=ManyImgs(0.6,[img1,imgcontours]) # cv2.imshow("res",imgsres # ) # cv2.waitKey(0)