将形状复制到空白画布

将形状复制到空白画布

本文介绍了将形状复制到空白画布(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

源图像:

我正在使用此代码执行以下操作:1.创建40x40像素的白色画布2.使用Opencv函数findContours找到数字的轮廓(在本例中为5).

I am using this code to:1. create a white canvas of 40x40 pixels2. found the contours of number (in this case 5) using Opencv function findContours.

我想做的就是将此形状(请不要将边界框或矩形,而不是蓝色形状)复制到画布中.

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

经过一番研究,我了解到opencv映像只是一个numpy数组.从理论上讲,此数组应在新图像(我的白色画布..)中转换,然后使用数组中的值重建形状.我是对的吗?

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 ?

有人知道该怎么做吗?在某些情况下,围绕数字创建边界框/矩形可能会导致结果不准确.请不要把它作为解决方案.我已经以至少3-4种不同的方式进行了此过程,但结果还不够令人满意.

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..

谢谢.

推荐答案

对于轮廓图像

我认为想要类似

对于打开的数字,例如125,很容易做到:从整个图像中裁剪,或绘制新图像.对于0689之类的闭合数字,需要执行更多步骤.这是5的示例,您将获得.

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 .

详细信息和描述在代码中.

#!/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)

这篇关于将形状复制到空白画布(OpenCV,Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 23:38