赞
踩
- # -*- coding:utf-8 -*-
- import cv2
-
- img = cv2.imread('C:\\Users\\wu\\Desktop\\111VS.png') # 读取图片
- img = cv2.resize(img, (1000, 800)) # 裁剪图片
- imgContour = img.copy() # 复制图片
- img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 图片二值化
- minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(img_gray) # 获取最大最小像素及坐标
-
- cv2.drawMarker(img, minLoc, (0, 255, 0), markerType=0) # 绘制 0 + 十字
- cv2.drawMarker(img, maxLoc, (255, 0, 255), markerType=0)
- cv2.putText(img, text="min", org=minLoc, fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=1, color=(255, 0, 0), thickness=2)
- # putText各参数依次是:图片,添加的文字,左上角坐标,字体,字体大小,颜色黑,字体粗细
- cv2.putText(img, text="max", org=maxLoc, fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=1, color=(255, 0, 255),
- thickness=2)
-
- print('最小值:{}, 位置:{}'.format(minVal, minLoc))
- print('最大值:{}, 位置:{}'.format(maxVal, maxLoc))
-
- cv2.imshow("Image", img)
- cv2.waitKey(0)
- # -*- coding:utf-8 -*-
- import cv2
- import numpy as np
-
- img = cv2.imread('C:\\Users\\wu\\Desktop\\test.bmp') # 读取图片
- img = cv2.resize(img, (300, 200)) # 裁剪图片
- imgContour = img.copy() # 复制图片
- gray = cv2.cvtColor(imgContour, cv2.COLOR_BGR2GRAY) # 图片二值化
- # 图像阈值处理
- ret, thresh1 = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
- ret, thresh2 = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV)
- ret, thresh3 = cv2.threshold(gray, 127, 255, cv2.THRESH_TRUNC)
- ret, thresh4 = cv2.threshold(gray, 127, 255, cv2.THRESH_TOZERO)
- ret, thresh5 = cv2.threshold(gray, 127, 255, cv2.THRESH_TOZERO_INV)
- ret, thresh6 = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
- ret, thresh7 = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
- ret, thresh8 = cv2.threshold(gray, 127, 255, cv2.THRESH_TRUNC + cv2.THRESH_OTSU)
- ret, thresh9 = cv2.threshold(gray, 127, 255, cv2.THRESH_TOZERO + cv2.THRESH_OTSU)
- ret, thresh10 = cv2.threshold(gray, 127, 255, cv2.THRESH_TOZERO_INV + cv2.THRESH_OTSU)
- ret, thresh11 = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY + cv2.THRESH_TRIANGLE)
- ret, thresh12 = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_TRIANGLE)
- ret, thresh13 = cv2.threshold(gray, 127, 255, cv2.THRESH_TRUNC + cv2.THRESH_TRIANGLE)
- ret, thresh14 = cv2.threshold(gray, 127, 255, cv2.THRESH_TOZERO + cv2.THRESH_TRIANGLE)
- ret, thresh15 = cv2.threshold(gray, 127, 255, cv2.THRESH_TOZERO_INV + cv2.THRESH_TRIANGLE)
-
- imgHor1 = np.hstack((thresh1, thresh2, thresh3, thresh4))
- imgHor2 = np.hstack((thresh5, thresh6, thresh7, thresh8))
- imgHor3 = np.hstack((thresh9, thresh10, thresh11, thresh12))
- imgHor4 = np.hstack((thresh13, thresh14, thresh15, gray))
- Image = np.vstack((imgHor1, imgHor2,imgHor3,imgHor4))
- cv2.imshow("Image", Image)
- cv2.waitKey(0)
- # -*- coding:utf-8 -*-
- import cv2
- import numpy as np
- img = cv2.imread('C:\\Users\\wu\\Desktop\\111VS.png') # 读取图片
- img = cv2.resize(img, (1000, 800)) # 裁剪图片
- img1 = img.copy()
- img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 图片二值化
-
- (_, im_bw) = cv2.threshold(img_gray, 100, 255,
- cv2.THRESH_BINARY | cv2.THRESH_OTSU) # 阈值处理 凸包cv2.THRESH_OTSU cv2.THRESH_TRIANGLE
- (cnts, _) = cv2.findContours(im_bw.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) # 提取轮廓
-
- cnt = max(cnts, key=cv2.contourArea) # 提取最大轮廓
- cv2.drawContours(img, cnt, -1, (128, 0, 128), 2) # 绘制轮廓 -1 全部绘制 颜色 线条粗细
-
- hull = cv2.convexHull(cnt, returnPoints=True) # 凸包
- cv2.polylines(img, [hull], True, (128, 0, 128), 2) # 绘制凸包
-
- (_, im_bw) = cv
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。