python-opencv创建空白图
程序员文章站
2022-06-02 20:09:00
...
使用 python-opencv 创建背景图片,进行边缘提取出。
创建空白图片
单通道
import cv2
import numpy as np
img = np.zeros((10, 10), np.uint8)
# 浅灰色背景
img.fill(200)
cv2.imshow('img', img)
cv2.waitKey(0)
三通道彩色图
import cv2
import numpy as np
img = np.zeros((10, 10, 3), np.uint8)
# 浅灰色背景
img.fill(200)
cv2.imshow('img', img)
cv2.waitKey(0)
使用背景图参与运算
import cv2
import numpy as np
img = cv2.imread('./test.jpg')
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
Y_min=np.array([20,120,110])
Y_max=np.array([30,255,180])
img_bin = np.inRange(hsv, Ymin, Y_max)
model = np.zeros((img.shape[0], img.shape[1]))
contours, hierarchy = cv2.findContours(img_bin.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
报错:
cv2.error: OpenCV(4.2.0) /io/opencv/modules/imgproc/src/contours.cpp:197: error: (-210:Unsupported format or combination of formats) [Start]FindContours supports only CV_8UC1 images when mode != CV_RETR_FLOODFILL otherwise supports CV_32SC1 images only in function 'cvStartFindContours_Impl
修改:
import cv2
import numpy as np
img = cv2.imread('./test.jpg')
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
Y_min=np.array([20,120,110])
Y_max=np.array([30,255,180])
img_bin = np.inRange(hsv, Ymin, Y_max)
- model = np.zeros((img.shape[0], img.shape[1]))
+ model = np.zeros((img.shape[0], img.shape[1]), np.uint8)
contours, hierarchy = cv2.findContours(img_bin.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
上一篇: 给实体类单独设置json序列化策略
下一篇: python打印图像所有的像素值