我如何在`opencv`中访问轮廓的顺序 [英] How can i access the ordering of contours in `opencv`
问题描述
import cv2
import Image
import numpy as np
#improve image..........................................................
im = cv2.imread('bw_image1.jpg')
gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),0)
thresh = cv2.adaptiveThreshold(blur,255,1,1,11,2)
contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
i=0
for cnt in contours:
[x,y,w,h] = cv2.boundingRect(cnt)
if h>15:
cv2.rectangle(im,(x,y),(x+w,y+h),(0,0,255),1)
im3=im[y:y+h,x:x+w]
cv2.imwrite('objects/pix%i.png'%i,im3)
i+=1
cv2.imshow('norm',im)
cv2.imwrite('objects/shhh.jpg',im)
key = cv2.waitKey(0)
#adding object............
im0 = cv2.imread('objects/pix0.png',0)
im1 = cv2.imread('objects/pix1.png',0)
im2 = cv2.imread('objects/pix2.png',0)
im3 = cv2.imread('objects/pix3.png',0)
im4 = cv2.imread('objects/pix4.png',0)
im5 = cv2.imread('objects/pix5.png',0)
h0, w0 = im0.shape[:2]
h1, w1 = im1.shape[:2]
h2, w2 = im2.shape[:2]
h3, w3 = im3.shape[:2]
h4, w4 = im4.shape[:2]
h5, w5 = im5.shape[:2]
maxh=max(h0,h1,h2,h3,h4,h5)
#add 50 for space between the objects
new = np.zeros((maxh, w0+w1+w2+w3+w4+w5+5),np.uint8)
new=(255-new)
new[maxh-h0:, :w0] = im0
new[maxh-h1:, w0+1:w0+w1+1] = im1
new[maxh-h2:, w0+w1+2:w0+w1+w2+2] = im2
new[maxh-h3:, w0+w1+w2+3:w0+w1+w2+w3+3] = im3
new[maxh-h4:, w0+w1+w2+w3+4:w0+w1+w2+w3+w4+4] = im4
new[maxh-h5:, w0+w1+w2+w3+w4+5:] = im5
gray = cv2.cvtColor(new, cv2.COLOR_GRAY2BGR)
cv2.imshow('norm',gray)
cv2.imwrite('objects/new_image.jpg',gray)
key = cv2.waitKey(0)
# threshold ................................................
im_gray = cv2.imread('objects/new_image.jpg', cv2.CV_LOAD_IMAGE_GRAYSCALE)
(thresh, im_bw) = cv2.threshold(im_gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
thresh = 20
im_bw = cv2.threshold(im_gray, thresh, 255, cv2.THRESH_BINARY)[1]
cv2.imwrite('bw_image1.jpg', im_bw)
im = Image.open('bw_image1.jpg')
im2 = im.resize((300, 175), Image.NEAREST)
im2.save('bw_image1.jpg')
我正在使用上面的代码对图像进行重新排序
I am using above code to reordering a image
问题在于最终结果图片是不依次保存主图片.
The problem is in final result image is not saving in sequence of main image.
谁能告诉我怎么做?
主图像:-
结果图片:-
主图像和结果图像单词应看起来相同.预先感谢
main image and the result image word should look like same. Thank in advance
推荐答案
Opencv
从图像底部找到轮廓.因此,当您尝试查找图像的轮廓时,如下所示:
Opencv
find the contours from bottom of the image . so when you try to find the contours of an image like this :
第一个轮廓用于8
(3
的下一位),然后是3
,7
,9
,4
,e
,没有常规的配方可以找到轮廓顺序.因此,我们需要基于对象的x
存储对象,并增加了从左到右x
的方法,因此可以在找到conturs之后使用以下代码存储已创建的对象:
the first contour are for 8
(a bit is lower of 3
) then 3
,7
,9
,4
,e
there is not a regular recipe for find the order of contours . so we need to store objects based on theirs x
, with this method that from left to right x
has been increased , so we can use the below code to store the founded objects after find conturs :
import numpy as np
import cv2
im = cv2.imread('nnn.jpg')
gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),0)
thresh = cv2.adaptiveThreshold(blur,255,1,1,19,4)
contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
h_list=[]
for cnt in contours:
[x,y,w,h] = cv2.boundingRect(cnt)
if w*h>250:
h_list.append([x,y,w,h])
#print h_list
ziped_list=zip(*h_list)
x_list=list(ziped_list[0])
dic=dict(zip(x_list,h_list))
x_list.sort()
i=0
for x in x_list:
[x,y,w,h]=dic[x]
#cv2.rectangle(im,(x,y),(x+w,y+h),(0,0,255),1)
im3=im[y:y+h,x:x+w]
cv2.imwrite('objects/pix%i.png'%i,im3)
i+=1
cv2.imshow('norm',im)
cv2.imwrite('objects/shhh.jpg',im)
key = cv2.waitKey(0)
注意,注释了行#cv2.rectangle(im,(x,y),(x+w,y+h),(0,0,255),1)
拒绝了结果图像中的多余行!
Note the line #cv2.rectangle(im,(x,y),(x+w,y+h),(0,0,255),1)
has been commented for refusing of extra lines in result image !
然后使用以下代码连接保存的对象:
then concatenate the saved objects whit this code :
import numpy as np
import cv2
im0 = cv2.imread('objects/pix0.png',0)
im1 = cv2.imread('objects/pix1.png',0)
im2 = cv2.imread('objects/pix2.png',0)
im3 = cv2.imread('objects/pix3.png',0)
im4 = cv2.imread('objects/pix4.png',0)
im5 = cv2.imread('objects/pix5.png',0)
h0, w0 = im0.shape[:2]
h1, w1 = im1.shape[:2]
h2, w2 = im2.shape[:2]
h3, w3 = im3.shape[:2]
h4, w4 = im4.shape[:2]
h5, w5 = im5.shape[:2]
maxh=max(h0,h1,h2,h3,h4,h5)
#add 50 for space between the objects
new = np.zeros((maxh, w0+w1+w2+w3+w4+w5+50),np.uint8)
new=(255-new)
new[maxh-h0:, :w0] = im0
new[maxh-h1:, w0+10:w0+w1+10] = im1
new[maxh-h2:, w0+w1+20:w0+w1+w2+20] = im2
new[maxh-h3:, w0+w1+w2+30:w0+w1+w2+w3+30] = im3
new[maxh-h4:, w0+w1+w2+w3+40:w0+w1+w2+w3+w4+40] = im4
new[maxh-h5:, w0+w1+w2+w3+w4+50:] = im5
gray = cv2.cvtColor(new, cv2.COLOR_GRAY2BGR)
cv2.imshow('norm',gray)
cv2.imwrite('objects/new_image.jpg',gray)
key = cv2.waitKey(0)
结果:
这篇关于我如何在`opencv`中访问轮廓的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!