opencv python 生成画布,将形状复制到空白画布(OpenCV,Python)

import numpy as np

import cv2

blank_image = np.zeros((40,40,3), np.uint8)

blank_image.fill(255)

#cv2.imshow('i', blank_image)

#cv2.waitKey(0)

im = cv2.imread('img.png')

imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)

ret, thresh = cv2.threshold(imgray, 127, 255, 0)

image, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

cnt = contours[4]

cnts = cv2.drawContours(im,[cnt],0,(255,0,0), -1)

cv2.imshow('i', im)

cv2.waitKey(0)

for a in cnt:

print(a) #this contour is a 3D numpy array

Source image:

4f0c5e971b14ca6d5e875d499596ad42.png

I am using this code to:

1. create a white canvas of 40x40 pixels

2. found the contours of number (in this case 5) using Opencv function findContours.

95743eb7a6453ba53e2b7ef135389a2d.png

What I want to do is to copy this shape (please, not the bounding box or rectangle, the blue shape) into the canvas.

After some research I learned that an opencv image is just a numpy array. This array, theoretically, should be translated in the new image (my white canvas..) and then reconstruct the shape using the values inside the array. I am right ?

Someone know how to do that ? Creating a bounding box / rectangle around the numbers would, in some cases, result inaccurate. Please, don't give that as solution. I already did this process in atleast 3-4 different ways and the results are not satisfactory enough.

So, the desired output would be something like this..

6ca66816162fb60d3a5049d5da0e0fde.png

Thanks.

解决方案

For contours image a6fa5bf8b94acd113d9e364fd3fb779e.png

I think want something like 1ca2165b18ec5134021758fc3197f9bb.png

For opened number such as 1, 2, 5 , it is easy to do: Crop from the whole image, or draw on new image. For closed number such as 0, 6, 8, 9, more steps are needed. Here is example for 5, you will get db24755c9efe3b1c225119b8cba3b938.png.

Details and description are in the code.

#!/usr/bin/python3

# 2018.01.14 09:48:15 CST

# 2018.01.14 11:39:03 CST

import numpy as np

import cv2

im = cv2.imread('test.png')

imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)

ret, thresh = cv2.threshold(imgray, 127, 255, 0)

contours = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[-2]

## this contour is a 3D numpy array

cnt = contours[4]

res = cv2.drawContours(im,[cnt],0,(255,0,0), -1)

cv2.imwrite("contours.png", res)

## Method 1: crop the region

x,y,w,h = cv2.boundingRect(cnt)

croped = res[y:y+h, x:x+w]

cv2.imwrite("croped.png", croped)

## Method 2: draw on blank

# get the 0-indexed coords

offset = cnt.min(axis=0)

cnt = cnt - cnt.min(axis=0)

max_xy = cnt.max(axis=0) + 1

w,h = max_xy[0][0], max_xy[0][1]

# draw on blank

canvas = np.ones((h,w,3), np.uint8)*255

cv2.drawContours(canvas, [cnt], -1, (255,0,0), -1)

cv2.imwrite("canvas.png", canvas)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值