[画像処理知識レビュー] 03コントラスト線形ストレッチMATLAB、C ++実装



03 Contrast Linear Stretching Matlab



アルゴリズム:(1)、灰色のヒストグラムを描画する クリックしてリンクを開きます ターゲットのグレー範囲を推定します。 (2)、勾配を見つけます。 勾配が1より大きい場合は、ストレッチします。 (3)一次方程式に従って、ストレッチ後のグレー値を見つけます。 以下に示すように





1. Matlabコード:



% Title: Contrast stretch % Significance: The object to be observed is not clear enough due to insufficient contrast, and contrast stretching is required. % Piecewise linear mapping of gray values % Known conditions: the target scene gray range [fa, fb] of the original image, the range after stretching is [ga, gb]. % Realization method: the target scene gray range stretching k2=(gb-ga)/(fb-fa), other suppression k1=ga/fa, k3=(255-gb)/(255-fb) % Difficulty: piecewise linear function solution. clc clear image = rgb2gray(imread('D:/Code/Image/classic.jpg')) figure,imshow(image) %1, by drawing a grayscale histogram, estimate the grayscale range of the target. fa = 75 % The abscissa, the target gray interval [75,150] fb = 150 ga = 30 % The ordinate, that is, the target gray interval after stretching [30,200], the dynamic range is expanded gb = 200 %2, find the slope, that is, the slope is greater than 1, then stretch k1 = ga/fa k2 = (gb-ga)/(fb-fa) k3 = (255-gb)/(255-fb) %3, find the gray value after stretching [row,col] = size(image) g = zeros(row,col)% default g is double. g = f g is uint8 for i=1:row for j=1:col if 0<=image(i,j) && image(i,j)<=fa g(i,j) = k1*image(i,j) else if fa<=image(i,j) && image(i,j)<=fb g(i,j) = k2*(image(i,j)-fa)+ga else if fb<=image(i,j) && image(i,j)<=255 g(i,j) = k3*(image(i,j)-fb)+gb end end end end end figure,imshow(uint8(g))%Because the statement g is double, it must be converted to uint8, because the image gray range [0,255] % imshow shows that the double type is in the range of 0~1, so it is displayed as white when it is greater than 1, and the number converted from uint8 is greater than or equal to 1 except for 0. % imshow shows the range of 0~255 when displaying uint8 type. % When using uint8 type during programming, the result of subtraction is not negative, there is precision problem when multiplying and dividing, but uint8 type takes up less memory.

効果画像:



2. C ++の実装

#include using namespace cv int main() { Mat img = imread('D:/Code/Image/classic.jpg',0) imshow('Original image',img) //Assuming that the target gray range is [75,150], it needs to be stretched to [30,200] int x1 = 75, y1 = 30, x2 = 150, y2 = 200 int x //Current gray value //Mat y = img.clone() //Grayscale value after stretching Mat y = Mat::zeros(img.size(), img.type()) //Grayscale value after stretching double k1 = y1 / x1, k2 = (y2 - y1) / (x2 - x1), k3 = (255 - y2) / (255 - x2) for (int i = 0 i

効果画像:


グレーレベルウィンドウ:アルゴリズムは、区分的線形関数が異なることを除いて、コントラスト線形ストレッチと同じです。セグメント化された線形マッピング。ターゲットのコントラストを高めるために、他のグレースケールは0に設定されます(つまり、背景は完全に黒です)。

グレーレベルウィンドウスライス:アルゴリズムはグレーレベルウィンドウと同じで、区分的線形関数が異なり、ターゲットは単純に0に設定され、背景は引き続き255に設定されます。つまり、2値化です。