python-opencv创建空白图

使用 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)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值