简体   繁体   中英

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.

Can anyone tell me how to do it ?

Main image :-

在此处输入图片说明

Result image :-

在此处输入图片说明

main image and the result image word should look like same. Thank in advance

Opencv find the contours from bottom of the image . so when you try to find the contours of an image like this : 在此处输入图片说明

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)

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)

result:

在此处输入图片说明

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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