首页 > 编程知识 正文

python图像处理opencv(opencv遍历图像像素)

时间:2023-05-06 20:03:02 阅读:74419 作者:2086

import c v2 importmatplotlib.pyplotaspltimportnumpyasnpath='./temp (1)/temp/src.jpg'threshold=80读取图像img=cvvvp

#灰度转换img_gray=cv2.cvtcolor(img,cv2.COLOR_BGR2GRAY ) img _ gray.shape PLT.im show (img _ gray,cmap=' )

#高斯模糊img _ gray=cv2.Gaussian blur (img _ gray,(3,3 ),1 ) PLT.show (img _ gray,cmap='gray ' ) PLT.axis

二值变换_,img_br=cv2.Threshold(img_gray,threshold,255,cv2.THRESH_BINARY ) plt.show ) img_br,c marard

#寻找外部轮廓contours,hierarchy=cv2.find contours (img _ br,cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE )确保至少找到一个2 ) iflen(contours ) 0: # ) cnts=sorted ) contours,key=cv2.coourted reverse=True ) for c in cnts: # )按轮廓大小降序排列True ) approx=cv2.approxpolydp(c,0.02 * peri, True ) #近似轮廓有四个点,iflen(approx )==4: contour=approx break#轮廓图像img_contour=img.copy ) cv2.Drawcontours 确定保留1 ) cv2.imwrite('./contour.png ',img_contour ) cour=cv2.imread ) './contour.png ' ) plt.show

#读取目标图像dst=cv2.imread('./temp(1)/temp/dst.jpg ) ) dst=cv2.imread('./temp )1)/temp/7.jpg

#获取初始化原图maskmask=NP.Zeros_like(img ) )子图的纵横,赋予maskh,w,c=dst.shapemask(:wh,w, )=255 0]、[0,h - 1]、[w - 1,h - 1]、[w - 1,h - 1]、0 ) )目标掩码点point2=NP.float 32 (contour.reshape (4, 2 ) ) )得到变换矩阵perspective _ matrix=cv2.getperspectivetransfoor的point2) #里黑白mask _ center _ true=cv2.warp perspectitior

# mask_center_ROI=cv2.bitwise _ and (img )出图,然后绘制mask_center_true(PLT.imshow ) mask _ center _ ROI 反取mask temp=NP.ones 255 mask _ center _ false=cv2.bitwise _ xor (mask _ center _ true, temp ) PLT.imshow(mask_center_false ) PLT.axis ) off (PLT.show ) # andsrc _ center _ false=cv2.bitwwith mask _ center _ false (PLT.im show ) src _ center _ false (PLT.axis ) PLT . 得到dst透射变换temp=NP.zee MP 3360 )=dst dst _ center _ true=cv2.warp perspective (temp,perspective_matrix,(img.)

#融合result=cv2.bitwise _ or (src _ center _ false [ :33603360-1 ],dst_center_true ) PLT.im

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。