[PYTHON] Binarize photo data with OpenCV

I researched because I wanted to convert the photo data into black and white binary and input it to Blender.

The following sample program is used as a reference.

python - Converting an OpenCV Image to Black and White - Stack Overflow

Display binarized file

import cv as cv1
import cv2 as cv
import numpy as np
import sys

argvs = sys.argv

#Photo file
src = argvs[1]
#output data
output = argvs[2] 

im_gray = cv.imread(src, cv.CV_LOAD_IMAGE_GRAYSCALE)
im_gray_mat = cv1.fromarray(im_gray)
im_bw = cv1.CreateImage(cv1.GetSize(im_gray_mat), cv1.IPL_DEPTH_8U, 1)
im_bw_mat = cv1.GetMat(im_bw)

threshold = 0
cv1.Threshold(im_gray_mat, im_bw_mat, threshold, 255, cv1.CV_THRESH_BINARY | cv1.CV_THRESH_OTSU)

cv.imshow('gray image', np.asarray(im_bw_mat))
cv.waitKey(0)
cv.destroyAllWindows()

Data output of binarized matrix with savetxt

import cv as cv1
import cv2 as cv
import numpy as np
import sys

argvs = sys.argv

src = argvs[1] 
output = argvs[2]

im_gray = cv.imread(src, cv.CV_LOAD_IMAGE_GRAYSCALE)
im_gray_mat = cv1.fromarray(im_gray)
im_bw = cv1.CreateImage(cv1.GetSize(im_gray_mat), cv1.IPL_DEPTH_8U, 1)
im_bw_mat = cv1.GetMat(im_bw)

threshold = 0
cv1.Threshold(im_gray_mat, im_bw_mat, threshold, 255, cv1.CV_THRESH_BINARY | cv1.CV_THRESH_OTSU)

print np.asarray(im_bw_mat)
a = np.asarray(im_bw_mat)

np.savetxt(output, a, fmt='%10.5f', delimiter='\t')

I felt that I would need linear algebra.

Recommended Posts

Binarize photo data with OpenCV
Data Augmentation with openCV
[OpenCV] Personal identification with face photo
Data analysis with python 2
Binarization with OpenCV / Python
Visualize data with Streamlit
Data visualization with pandas
Data manipulation with Pandas!
Rotate sprites with OpenCV
Shuffle data with pandas
Normarize data with Scipy
Data analysis with Python
LOAD DATA with PyMysql
Easy TopView with OpenCV
Stumble with homebrew opencv3
Sample data created with python
Graph Excel data with matplotlib (1)
Artificial data generation with numpy
Face recognition with Python's OpenCV
"Apple processing" with OpenCV3 + Python3
Extract Twitter data with CSV
Image editing with python OpenCV
Camera capture with Python + OpenCV
Get Youtube data with python
[Python] Using OpenCV with Python (Basic)
Clustering ID-POS data with LDA
Learn new data with PaintsChainer
Loop video loading with opencv
Graph Excel data with matplotlib (2)
Real-time edge detection with OpenCV
Save tweet data with Django
Face detection with Python + OpenCV
Get image features with OpenCV
Face recognition / cutting with OpenCV
Try OpenCV with Google Colaboratory
Cascade classifier creation with opencv
Data processing tips with Pandas
Using OpenCV with Python @Mac
Interpolate 2D data with scipy.interpolate.griddata
Image recognition with Keras + OpenCV
Read json data with python
Anime face detection with OpenCV
Shining life with Python and OpenCV
Save & load data with joblib, pickle
Neural network with OpenCV 3 and Python 3
[Python] Using OpenCV with Python (Image transformation)
[Python] Using OpenCV with Python (Edge Detection)
Photo segmentation and clustering with DBSCAN
Erase certain colors with OpenCV + PySimpleGUI
How to deal with imbalanced data
Introducing OpenCV on Mac with homebrew
How to deal with imbalanced data
Versatile data plotting with pandas + matplotlib
Easy Python + OpenCV programming with Canopy
Finding the simplest mistakes with OpenCV
Python data structures learned with chemoinformatics
Install the data files with setup.py
Parse pcap data with tshark command
Try face recognition with python + OpenCV
Create noise-filled audio data with SoX
How to Data Augmentation with PyTorch