Python implementation of image vertical projection example

Time:2020-9-20

Python + opencv direct code

import cv2 
import numpy as np 
from matplotlib import pyplot as plt 
from PIL import Image
 
IMG = CV2. Imread ('0002. JPG ') ා read the image and replace it with an operational array
GrayImage=cv2.cvtColor(img,cv2.COLOR_ Bgr2gray) ා convert BGR image to grayscale image
ret,thresh1=cv2.threshold(GrayImage,130,255,cv2.THRESH_ Binary) ා binarize the image (130255) and change the points between them to 255 (background)
#Print (thresh1 [0,0]) ා 250 outputs the pixel value of [0,0] ා the return value RET is the threshold value
# print(ret)#130
(h, w) = thresh1. Shape ා returns height and width
#Print (h, w) ා s output height and width
a = [0 for z in range(0, w)] 
Print (a) # a = [0,0,0,0,0,0,0,0,0,0,0,..., 0,0] initializes an array of length W, which is used to record the number of black spots in each column 
 
#Record the crest of each column
For J in range (0, w): traverse a column 
  For I in range (0, H): ා traverse a row
    If thresh1 [I, J] = = 0: ා if the point is changed to a black point
      The count of this counter is 1
      Thresh1 [I, J] = 255 ා turns white after recording 
  # print (j)      
 
#      
For J in range (0, w): traverse each column
  For I in range ((H-A [J]), H): ා start at the top point of the column that should be blackened to the bottom
    Thresh1 [I, J] = 0 ᦇ blackening
 
#Thresh1 is a histogram of the image projected vertically
#If you want to segment characters, in fact, you don't need to draw this diagram, you just need to a = [], you can get the information you want
 
 
# img2 =Image.open('0002.jpg')
# img2.convert('L')
# img_1 = np.array(img2)
plt.imshow(thresh1,cmap=plt.gray())
plt.show()
cv2.imshow('img',thresh1) 
cv2.waitKey(0) 
cv2.destroyAllWindows()

Original image:

Operation results:

Projection in the horizontal direction, the code is as follows (the principle is the same as above)

import cv2 
import numpy as np 
from matplotlib import pyplot as plt 
from PIL import Image
 
img=cv2.imread('C:/Users/Jet Zhang/Desktop/50/50/cut.png') 
GrayImage=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 
ret,thresh1=cv2.threshold(GrayImage,130,255,cv2.THRESH_BINARY)
 
(h, w) = thresh1. Shape ා returns height and width
 
a = [0 for z in range(0, h)] 
print(a) 
 
for j in range(0,h): 
  for i in range(0,w): 
    if thresh1[j,i]==0: 
      a[j]+=1 
      thresh1[j,i]=255
     
for j in range(0,h): 
  for i in range(0,a[j]):  
    thresh1[j,i]=0  
 
plt.imshow(thresh1,cmap=plt.gray())
plt.show()

The effect picture is as follows:

The above Python image vertical projection example is all the content that Xiaobian shared with you. I hope to give you a reference, and I hope you can support developeppaer more.

Recommended Today

The first Python Programming challenge on the Internet (end)

Date of establishment: March 28, 2020Update Date: April 22, 2020 (end)Personal collection Tool.pywebsite:http://www.pythonchallenge.com/Note: please quote or change this article at will, just mark the source and the author. The author does not guarantee that the content is absolutely correct. Please be responsible for any consequences Title: the first Python Programming challenge on the web Find […]