My First OpenCV program by Python-ImgAutoBin

I have learned Python for half a day and I think I can make something out of python.Then I thought about OpenCV.How about a program that can turn RGB image into black-and-white image?I began working and that is what I have done.

Original Image:

test.jpg

Converted:

testout.jpg

NOTICE:This image is from remartarchive.tumblr.com

And that’s auto!

The code is here:

from PIL import Image
from pylab import *
import cv2
from optparse import OptionParser
from matplotlib import pyplot as plt
import Tkinter
import FileDialog
from lipytools import denoise
isize = 128

parser = OptionParser()

parser.add_option(“-f”, “–file”, dest=”filenm”,
type=”string”,
help=”Select input file”)

parser.add_option(“-d”, “–delta”,
type=”int”, dest=”delta”,
help=”Select delta”)

parser.add_option(“-p”, “–percent”,
type=”float”, dest=”percent”,
help=”Select float”)

(options, args) = parser.parse_args()
filename = options.filenm
percent = options.percent
delta = options.delta

print(“Receiving data.”)
imi2 = Image.open(filename).convert(‘L’)

print(“Compressing data.”)
(x,y) = imi2.size
x_s = isize
y_s = y * x_s / x
im2 = array(imi2)
im4 = 255.0 * (im2/255.0)**2
imi4 = Image.fromarray(im4)
imi4 = imi4.resize((x_s,y_s),Image.ANTIALIAS)
im4 = array(imi4)

# print(“Denoising data.”)
# U,T = denoise(im4,im4)
# figure()
# imshow(im4)
# figure()
# imshow(U)
# show()

print(“Converting data.”)
x=x_s
y=y_s
data=[]
while True:
data.append(int(im4[y-1,x-1]))
if x==0 :
if y==0:
break
else:
x = len(im4[1])
y-=1
else:
x-=1

print(“data accepted.”)

print(“Analoging data.”)
whiter = 0
i=0
while True:
if data.count(i)>whiter:
print(str(i)+” is the whiter”)
whiter = data.count(i)
white = i
if i==255:
break
else:
i+=1
print(str(i) + ” is NOT a whiter”)

height = whiter
j = white

while True:
if data.count(j) < height * percent:
print(str(i) + ” is the blacker”)
black = j
break
else:
print(str(j)+” is NOT a blacker”)
j-=1

print(“Final result:”+str(white)+”,”+str(black))

imcv = cv2.imread(filename)
print(“Image read.”)
grey = cv2.cvtColor(imcv, cv2.COLOR_RGB2GRAY)
print(“Image greyed.”)
_,bin = cv2.threshold(grey,black-delta,255,cv2.THRESH_BINARY)
_,bin2 = cv2.threshold(grey,black,255,cv2.THRESH_BINARY)
print(“Image Bined.”)
contours, hierarchy = cv2.findContours(bin,cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE)
cv2.drawContours(bin2,contours,-1,(0,0,0),1)
print(“Image Bounded.”)

plt.imshow(bin2, cmap = ‘gray’, interpolation = ‘bicubic’)
plt.xticks([]), plt.yticks([])
plt.show()

You need a terminal to run it.

python testautobin.py -f "the file to input" -d "delta" -p "percent"

In fact,that’s a very simple program,and cannot really work out of human.You still need to make some modify.I encourage you to use delta = 50,percent = 0.02.

I have just found out the interesting of OpenCV and Python!I will try to do something more interesting!If you discovered bugs or butterflies,you can contact me!See you guys!

Leave a Reply

Your email address will not be published. Required fields are marked *