手机版 欢迎访问it开发者社区(www.mfbz.cn)网站

当前位置: > 开发

python-opencv创建空白图

时间:2021/5/18 17:36:57|来源:|点击: 次

使用 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)

Copyright © 2002-2019 某某自媒体运营 版权所有