Python
 Computer >> コンピューター >  >> プログラミング >> Python

OpenCVを使用して画像にバイナリしきい値を実行する


このプログラムでは、openCVを使用して画像に対してバイナリしきい値を実行します。

しきい値処理は、各ピクセルの値がしきい値に関連して変更されるプロセスです。ピクセルには、しきい値よりも小さい場合は特定の値が与えられ、しきい値よりも大きい場合は他の値が与えられます。バイナリしきい値処理では、ピクセルの値がしきい値よりも小さい場合、0の値、つまり黒が与えられます。しきい値よりも大きい場合は、255、つまり白が割り当てられます。

元の画像

OpenCVを使用して画像にバイナリしきい値を実行する

アルゴリズム

Step 1: Import cv2.
Step 2: Define threshold and max_val.
Step 3: Pass these parameters in the cv2.threshold value and specify the type of thresholding you want to do.
Step 4: Display the output.
>

サンプルコード

import cv2

image = cv2.imread('testimage.jpg')
threshold_value = 120
max_val = 255
ret, image = cv2.threshold(image, threshold_value, max_val, cv2.THRESH_BINARY)
cv2.imshow('BinaryThresholding', image)

出力

OpenCVを使用して画像にバイナリしきい値を実行する

説明

プログラムのret変数は、単にしきい値を返します。しきい値より大きい値を持つピクセルの場合、それらはmax_val、つまり255に置き換えられます。


  1. OpenCVを使用して画像に楕円を描画します

    このプログラムでは、OpenCVライブラリを使用して画像に楕円を描画します。同じようにOpenCV関数ellipse()を使用します。 元の画像 アルゴリズム Step 1: Import cv2. Step 2: Read the image using imread(). Step 3: Set the center coordinates. Step 4: Set the axes length. Step 5: Set the angle. Step 6: Set start and end angle. Step 6: Set the color. Step 7: Set th

  2. OpenCVを使用して画像に線を引く

    このプログラムでは、OpenCV関数line()を使用して画像に単純な線を描画します。 元の画像 アルゴリズム Step 1: Import cv2. Step 2: Read the image using imread(). Step 3: Get the dimensions of the image using the image.shape method. Step 4: Define starting point of the line. Step 5: Define the end point of the line. Step 6: Define the thicknes