9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

OpenCVで写真データを2値化

Posted at

写真データを白黒の2値にして、Blenderの入力にしたいと思い調査しました。

以下のサンプルプログラムを参考にしています。

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

2値化したファイルを表示

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)

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

2値化した行列をsavatxtでデータ出力

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')

線形代数とか、必要になるんだろうなと感じました。

9
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?