LoginSignup
0
0

More than 3 years have passed since last update.

OpenCV/Pythonで2値化

Last updated at Posted at 2020-05-28

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import cv2
import time

def conv():
    # 閾値の設定
    threshold = 100

    # 二値化(閾値100を超えた画素を255にする。)
    ret, img_thresh = cv2.threshold(img, threshold, 255, cv2.THRESH_BINARY)

    # 二値化画像の表示
    cv2.imshow("img_th", img_thresh)
    cv2.waitKey()
    cv2.destroyAllWindows()

def fps(video):
    fps = video.get(cv2.CAP_PROP_FPS)
    print("FPSの設定値、video.get(cv2.CAP_PROP_FPS) : {0}".format(fps))

    # 取得するフレームの数
    num_frames = 120

    print("取得中 {0} frames".format(num_frames))

    # 開始時間
    start = time.time()

    # フレームを取得する
    #for i in range(0, num_frames):
    #   ret, frame = video.read()

    # 終了時間
    end = time.time()

    # Time elapsed
    seconds = end - start
    print("経過時間: {0} seconds".format(seconds))

    # Calculate frames per second
    fps = num_frames / seconds
    print("計算したFPS : {0}".format(fps))

class FPS:
    def __init__(self):
        self.flag = False
        self.start = 0
        self.end = 0
        self.fps = 0
        self.framecnt = 0

    def calc_fps(self):
        if self.flag == False:
            self.start = time.time()
            self.flag = True        
        else:
            diff = time.time() - self.start
            if diff > 1.0:
                self.fps = self.framecnt / diff
                self.framecnt = 0
                self.start =  time.time()

        self.framecnt += 1
        return self.fps

def test1(mode):
    URL = "http://172.23.64.38:8081/?action=stream"
    winname ="winname"
    cv2.namedWindow(winname, cv2.WINDOW_NORMAL)

    try :
        if mode == 0:
            s_video = cv2.VideoCapture(0)
        else:
            s_video = cv2.VideoCapture(URL)


        # 閾値の設定
        threshold = 200
        fpsobj = FPS()

        while True:

            start = time.time()
            end = time.time()

            seconds = end - start

            ret, srcimg = s_video.read()
            #cv2.imshow(winname,img)
            key = cv2.waitKey(1) & 0xff
            if key == ord('q'): break

            # 二値化(閾値100を超えた画素を255にする。)
            #cv2.threshold(画像, 閾値, 閾値を超えた場合に変更する値, 二値化の方法)
            ret, img = cv2.threshold(srcimg, threshold, 255, cv2.THRESH_BINARY)

            ksize=7 # アパーチャーサイズ 3, 5, or 7 など 1 より大きい奇数。数値が大きいほどぼかしが出る。
            img = cv2.medianBlur(img,ksize)

            img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  # RGB2〜 でなく BGR2〜 を指定
            ret, img = cv2.threshold(img, 20, 255, cv2.THRESH_BINARY)


            #print("{}".format( fpsobj.calc_fps()))
            if ret:
                # 二値化画像の表示
                cv2.imshow(winname, img)
    except:
        pass

    cv2.destroyAllWindows()




if __name__ == "__main__":
    test1(1)

C++



#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <stdio.h>

#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/photo.hpp"

//    http://labs.eecs.tottori-u.ac.jp/sd/Member/oyamada/OpenCV/html/py_tutorials/py_imgproc/py_thresholding/py_thresholding.html

        Mat dst;
        int criteria = 100;
        int max = 255;


        for ( ;;) {

            cap.read( frame );
            cv::threshold( frame, dst, criteria, max, THRESH_BINARY );       // binalized


            int ksize = 7;// # アパーチャーサイズ 3, 5, or 7 など 1 より大きい奇数。数値が大きいほどぼかしが出る。
            cv::medianBlur( dst, dst, ksize );

            cv::cvtColor( dst, dst, COLOR_BGR2GRAY );// # RGB2〜 でなく BGR2〜 を指定
            cv::threshold( dst, dst, 20, max, THRESH_BINARY );


            imshow( "Live", dst );
            if ( waitKey( 5 ) >= 0 ) {
                break;
            }
        }



0
0
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
0
0