繁体   English   中英

我如何在`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')

我正在使用上面的代码对图像进行重新排序

问题在于最终结果图像没有 主图像顺序保存。

谁能告诉我该怎么做?

主图:-

在此处输入图片说明

结果图片:-

在此处输入图片说明

主图像和结果图像词应看起来相同。 预先感谢

Opencv从图像底部找到轮廓。 因此,当您尝试找到像这样的图像轮廓时: 在此处输入图片说明

第一轮廓是8 (比特是低3 )然后3794e没有常规配方找到轮廓的顺序。 因此,我们需要基于对象的x存储对象,并使用从左到右增加x方法,以便在找到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)已被注释为拒绝结果图像中的多余行!

然后使用以下代码连接保存的对象:

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)

结果:

在此处输入图片说明

import cv2
import numpy as np

im = cv2.imread('0.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('ob/pix%i.png' % i, im3)
        i += 1
cv2.imshow('norm', im)
key = cv2.waitKey(0)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM