mirror of
https://github.com/Laex/Delphi-OpenCV.git
synced 2024-11-15 07:45:53 +01:00
9a5de509ee
Signed-off-by: Laentir Valetov <laex@bk.ru>
464 lines
17 KiB
ObjectPascal
464 lines
17 KiB
ObjectPascal
(*
|
||
*****************************************************************
|
||
Delphi-OpenCV Demo
|
||
Copyright (C) 2013 Project Delphi-OpenCV
|
||
****************************************************************
|
||
Contributor:
|
||
Laentir Valetov
|
||
email:laex@bk.ru
|
||
****************************************************************
|
||
You may retrieve the latest version of this file at the GitHub,
|
||
located at git://github.com/Laex/Delphi-OpenCV.git
|
||
****************************************************************
|
||
The contents of this file are used with permission, subject to
|
||
the Mozilla Public License Version 1.1 (the "License"); you may
|
||
not use this file except in compliance with the License. You may
|
||
obtain a copy of the License at
|
||
http://www.mozilla.org/MPL/MPL-1_1Final.html
|
||
|
||
Software distributed under the License is distributed on an
|
||
"AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||
implied. See the License for the specific language governing
|
||
rights and limitations under the License.
|
||
*******************************************************************
|
||
*)
|
||
|
||
|
||
unit ocv.editor;
|
||
|
||
{$I OpenCV.inc}
|
||
|
||
interface
|
||
|
||
Uses
|
||
ocv.core.types_c;
|
||
|
||
procedure ImageToHSVPlane(const InputImage: pIplImage; var h_plane, s_plane, v_plane: pIplImage);
|
||
procedure HSVPlaneToImage(const h_plane, s_plane, v_plane: pIplImage; Var OutputImage: pIplImage);
|
||
|
||
// --------------------------------------- Íàñûùåííîñòü ------------------------------------------------------
|
||
{
|
||
Äëÿ èçìåíåíèÿ íàñûùåííîñòè èçîáðàæåíèå ïðåîáðàçóåòñÿ â ñèñòåìó öâåòíîñòè
|
||
HSV è ðàçáèâàåòñÿ íà ñëîè. Ê çíà÷åíèÿì ñëîÿ «Sature» ïðèáàâëÿåòñÿ øàã. Ñëîè îáúåäèíÿþòñÿ.
|
||
}
|
||
procedure Sature(const step: Integer; const InputImage: pIplImage; Var OutputImage: pIplImage);
|
||
// ------------------------------- Ýêñïîçèöèÿ ----------------------------------
|
||
{
|
||
Èçîáðàæåíèå ïðåîáðàçóåòñÿ â HSV è ðàçáèâàåòñÿ íà ñëîè.
|
||
Äëÿ ñëîÿ «Value» âûïîëíÿåì ïðåîáðàçîâàíèå ñ ïîìîùüþ ãèñòîãðàììû,
|
||
çàäàííîé ôóíêöèåé i + sin(i * 0.01255) * step * 10.
|
||
}
|
||
procedure Expo(const step: Integer; const InputImage: pIplImage; Var OutputImage: pIplImage);
|
||
// ---------------------------- Îòòåíîê -------------------------------------
|
||
{
|
||
Ïàðàìåòð îòòåíêà õàðàêòåðèçóåò íàëè÷èå â èçîáðàæåíèè çåëåíîãî è ïóðïóðíîãî öâåòà.
|
||
 ñèñòåìå öâåòíîñòè RGB ìîæíî óïðàâëÿòü çåëåíûì ñëîåì, íî ïðè ýòîì íóæíî íå çàáûâàòü
|
||
êîìïåíñèðîâàòü ïàäåíèå ÿðêîñòè äðóãèì äâóìÿ ñëîÿìè. Äëÿ ïðåîáðàçîâàíèÿ êðàñíîãî è
|
||
ñèíåãî ñëîåâ èñïîëüçóåòñÿ ïîëîæèòåëüíàÿ ãàììà-ôóíêöèÿ ýêñïîçèöèè, äëÿ çåëåíîãî – îòðèöàòåëüíàÿ.
|
||
}
|
||
procedure Hue(const step: Integer; const InputImage: pIplImage; Var OutputImage: pIplImage);
|
||
|
||
// ------------------------------------ Öâåòîâàÿ òåìïåðàòóðà ------------------------------
|
||
{
|
||
Èíãðåäèåíòû: òå æå, ÷òî è â îòòåíêå, íî ãèñòîãðàììû äëÿ êðàñíîãî è çåëåíîãî ïîëîæèòåëüíûå, à äëÿ ñèíåãî ñëîÿ äâîéíàÿ îòðèöàòåëüíàÿ.
|
||
Öâåòîâàÿ òåìïåðàòóðà õàðàêòåðèçóåò íàëè÷èå â èçîáðàæåíèè æåëòîãî è ñèíåãî öâåòîâ. Çíà÷èò áóäåì «êðóòèòü» ñèíèé.
|
||
}
|
||
|
||
procedure Temperature(const step: Integer; const InputImage: pIplImage; Var OutputImage: pIplImage);
|
||
|
||
// ------------------------------------ Ñâåò è òåíè ------------------------------
|
||
{
|
||
Ïàðàìåòð «ñâåò» õàðàêòåðèçóåò ÿðêîñòü ñâåòëûõ îáëàñòåé èçîáðàæåíèÿ,
|
||
à ïàðàìåòð «òåíè» — ÿðêîñòü òåìíûõ îáëàñòåé. Ïðåîáðàçîâûâàòü áóäåì êàíàë ÿðêîñòåé.
|
||
}
|
||
procedure White(const step: Integer; const InputImage: pIplImage; Var OutputImage: pIplImage);
|
||
procedure Shadow(const step: Integer; const InputImage: pIplImage; Var OutputImage: pIplImage);
|
||
// -------------------------------- Êîíòðàñò ----------------------------------
|
||
{
|
||
Êîíòðàñò îïðåäåëÿåòñÿ â ðàçíîñòè ÿðêîñòåé.
|
||
Ò.å. äëÿ óâåëè÷åíèÿ êîíòðàñòà íàì íóæíî ðàçäâèíóòü äèàïàçîí
|
||
ÿðêîñòåé îò öåíòðà ê êðàÿì. Ïðåîáðàçîâàíèå âûïîëíÿåòñÿ äëÿ âñåõ ñëîåâ.
|
||
}
|
||
procedure Contrast(const step: Integer; const InputImage: pIplImage; Var OutputImage: pIplImage);
|
||
// -------------------------------- Ðåçêîñòü ----------------------------------
|
||
{
|
||
Ðåçêîñòü (÷åòêîñòü) îïðåäåëÿåòñÿ âûäåëåíèåì îòäåëüíûõ ýëåìåíòîâ, èõ êîíòóðîâ.
|
||
Âåëè÷èíà, îáðàòíàÿ ðåçêîñòè – ðàçìûòîñòü.
|
||
 opencv äëÿ ðàçìûòèÿ èçîáðàæåíèÿ èñïîëüçóåì ôóíêöèþ blur,
|
||
ïðèíèìàþùóþ â êà÷åñòâå ïàðàìåòðîâ èñõîäíîå èçîáðàæåíèå, âûõîäíîå èçîáðàæåíèå,
|
||
è ðàçìåð ìàòðèöû ðàçìûòèÿ. Îò ðàçìåðà ìàòðèöû ðàçìûòèÿ è çàâèñèò ñèëà ðàçìûòèÿ.
|
||
Ýòîò ðàçìåð äîëæåí áûòü ÷åòíûì, ÷òîáû íå óêàçûâàòü âðó÷íóþ öåíòð ìàòðèöû.
|
||
×åòêîñòü â opencv ïðîùå âñåãî ïîâûñèòü ñ ïîìîùüþ ìàòðèöû ñâåðòêè, èñïîëüçóÿ
|
||
ñïåöèàëüíóþ äëÿ ýòîãî ìàòðèöó. Ôóíêöèÿ «filter2D», êîòîðàÿ ïðèíèìàåò èñõîäíîå
|
||
èçîáðàæåíèå, ðåçóëüòèðóþùåå èçîáðàæåíèå, êîëè÷åñòâî áèò íà çíà÷åíèå ìàòðèöû
|
||
ñâåðòêè, ìàòðèöó ñâåðòêè, âûïîëíÿåò íåïîñðåäñòâåííî ïðåîáðàçîâàíèå.
|
||
Èòàê, êàê áóäåò âûãëÿäåòü ìåòîä ïîâûøåíèÿ/ïîíèæåíèÿ ÷åòêîñòè.
|
||
}
|
||
|
||
procedure Clarity(const step: Integer; const InputImage: pIplImage; Var OutputImage: pIplImage);
|
||
|
||
implementation
|
||
|
||
Uses
|
||
Math,
|
||
ocv.core_c,
|
||
ocv.imgproc_c,
|
||
ocv.imgproc.types_c;
|
||
|
||
// ---------------------------------------------------------------------------------------------
|
||
procedure ImageToHSVPlane(const InputImage: pIplImage; var h_plane, s_plane, v_plane: pIplImage);
|
||
Var
|
||
hsv: pIplImage;
|
||
begin
|
||
hsv := cvCreateImage(cvGetSize(InputImage), IPL_DEPTH_8U, 3);
|
||
h_plane := cvCreateImage(cvGetSize(InputImage), IPL_DEPTH_8U, 1);
|
||
s_plane := cvCreateImage(cvGetSize(InputImage), IPL_DEPTH_8U, 1);
|
||
v_plane := cvCreateImage(cvGetSize(InputImage), IPL_DEPTH_8U, 1);
|
||
try
|
||
cvCvtColor(InputImage, hsv, CV_BGR2HSV_FULL);
|
||
cvCvtPixToPlane(hsv, h_plane, s_plane, v_plane, nil);
|
||
finally
|
||
cvReleaseImage(hsv);
|
||
end;
|
||
end;
|
||
|
||
procedure HSVPlaneToImage(const h_plane, s_plane, v_plane: pIplImage; Var OutputImage: pIplImage);
|
||
Var
|
||
hsv: pIplImage;
|
||
begin
|
||
hsv := cvCreateImage(cvGetSize(h_plane), IPL_DEPTH_8U, 3);
|
||
OutputImage := cvCreateImage(cvGetSize(h_plane), IPL_DEPTH_8U, 3);
|
||
try
|
||
cvCvtPlaneToPix(h_plane, s_plane, v_plane, nil, hsv);
|
||
cvCvtColor(hsv, OutputImage, CV_HSV2BGR_FULL);
|
||
finally
|
||
cvReleaseImage(hsv);
|
||
end;
|
||
end;
|
||
|
||
function AddDoubleToByte(const bt: byte; const d: double): byte;
|
||
begin
|
||
result := bt;
|
||
if (result + d) > 255 then
|
||
result := 255
|
||
else if (result + d) < 0 then
|
||
result := 0
|
||
else
|
||
result := result + Trunc(d);
|
||
end;
|
||
|
||
function GetGammaExpo(const step: Integer): pIplImage;
|
||
Var
|
||
i: Integer;
|
||
begin
|
||
result := cvCreateImage(cvSize(256, 1), IPL_DEPTH_8U, 1);
|
||
for i := 0 to 255 do
|
||
result^.imageData[i] := AddDoubleToByte(i, sin(i * 0.01255) * step * 10);
|
||
end;
|
||
|
||
// --------------------------------------- Íàñûùåííîñòü ------------------------------------------------------
|
||
{
|
||
Äëÿ èçìåíåíèÿ íàñûùåííîñòè èçîáðàæåíèå ïðåîáðàçóåòñÿ â ñèñòåìó öâåòíîñòè
|
||
HSV è ðàçáèâàåòñÿ íà ñëîè. Ê çíà÷åíèÿì ñëîÿ «Sature» ïðèáàâëÿåòñÿ øàã. Ñëîè îáúåäèíÿþòñÿ.
|
||
}
|
||
procedure Sature(const step: Integer; const InputImage: pIplImage; Var OutputImage: pIplImage);
|
||
Var
|
||
h_plane: pIplImage;
|
||
s_plane: pIplImage;
|
||
v_plane: pIplImage;
|
||
i, j: Integer;
|
||
begin
|
||
ImageToHSVPlane(InputImage, h_plane, s_plane, v_plane);
|
||
try
|
||
for i := 0 to s_plane^.width * s_plane^.height - 1 do
|
||
begin
|
||
j := s_plane^.imageData[i] + step * 5;
|
||
if j < 256 then
|
||
s_plane^.imageData[i] := j
|
||
else
|
||
s_plane^.imageData[i] := 255;
|
||
end;
|
||
HSVPlaneToImage(h_plane, s_plane, v_plane, OutputImage);
|
||
finally
|
||
cvReleaseImage(h_plane);
|
||
cvReleaseImage(s_plane);
|
||
cvReleaseImage(v_plane);
|
||
end;
|
||
end;
|
||
|
||
// ------------------------------- Ýêñïîçèöèÿ ----------------------------------
|
||
{
|
||
Èçîáðàæåíèå ïðåîáðàçóåòñÿ â HSV è ðàçáèâàåòñÿ íà ñëîè.
|
||
Äëÿ ñëîÿ «Value» âûïîëíÿåì ïðåîáðàçîâàíèå ñ ïîìîùüþ ãèñòîãðàììû,
|
||
çàäàííîé ôóíêöèåé i + sin(i * 0.01255) * step * 10.
|
||
}
|
||
procedure Expo(const step: Integer; const InputImage: pIplImage; Var OutputImage: pIplImage);
|
||
Var
|
||
h_plane: pIplImage;
|
||
s_plane: pIplImage;
|
||
v_plane: pIplImage;
|
||
_v_plane: pIplImage;
|
||
lut: pIplImage;
|
||
begin
|
||
ImageToHSVPlane(InputImage, h_plane, s_plane, v_plane);
|
||
_v_plane := cvClone(v_plane);
|
||
lut := GetGammaExpo(step);
|
||
try
|
||
cvLUT(v_plane, _v_plane, lut);
|
||
HSVPlaneToImage(h_plane, s_plane, _v_plane, OutputImage);
|
||
finally
|
||
cvReleaseImage(lut);
|
||
cvReleaseImage(h_plane);
|
||
cvReleaseImage(s_plane);
|
||
cvReleaseImage(v_plane);
|
||
cvReleaseImage(_v_plane);
|
||
end;
|
||
end;
|
||
|
||
// ---------------------------- Îòòåíîê -------------------------------------
|
||
{
|
||
Ïàðàìåòð îòòåíêà õàðàêòåðèçóåò íàëè÷èå â èçîáðàæåíèè çåëåíîãî è ïóðïóðíîãî öâåòà.
|
||
 ñèñòåìå öâåòíîñòè RGB ìîæíî óïðàâëÿòü çåëåíûì ñëîåì, íî ïðè ýòîì íóæíî íå çàáûâàòü
|
||
êîìïåíñèðîâàòü ïàäåíèå ÿðêîñòè äðóãèì äâóìÿ ñëîÿìè. Äëÿ ïðåîáðàçîâàíèÿ êðàñíîãî è
|
||
ñèíåãî ñëîåâ èñïîëüçóåòñÿ ïîëîæèòåëüíàÿ ãàììà-ôóíêöèÿ ýêñïîçèöèè, äëÿ çåëåíîãî – îòðèöàòåëüíàÿ.
|
||
}
|
||
procedure Hue(const step: Integer; const InputImage: pIplImage; Var OutputImage: pIplImage);
|
||
Var
|
||
h_plane, s_plane, v_plane: pIplImage;
|
||
_h_plane, _s_plane, _v_plane: pIplImage;
|
||
lut0, lut1, lut2: pIplImage;
|
||
begin
|
||
h_plane := cvCreateImage(cvGetSize(InputImage), IPL_DEPTH_8U, 1);
|
||
s_plane := cvCreateImage(cvGetSize(InputImage), IPL_DEPTH_8U, 1);
|
||
v_plane := cvCreateImage(cvGetSize(InputImage), IPL_DEPTH_8U, 1);
|
||
_h_plane := cvClone(h_plane);
|
||
_s_plane := cvClone(s_plane);
|
||
_v_plane := cvClone(v_plane);
|
||
try
|
||
cvCvtPixToPlane(InputImage, h_plane, s_plane, v_plane, nil);
|
||
lut0 := GetGammaExpo(step);
|
||
lut1 := GetGammaExpo(-step);
|
||
lut2 := GetGammaExpo(step);
|
||
cvLUT(h_plane, _h_plane, lut0);
|
||
cvLUT(s_plane, _s_plane, lut1);
|
||
cvLUT(v_plane, _v_plane, lut2);
|
||
OutputImage := cvCreateImage(cvGetSize(InputImage), InputImage^.depth, InputImage^.nChannels);
|
||
cvCvtPlaneToPix(_h_plane, _s_plane, _v_plane, nil, OutputImage);
|
||
finally
|
||
cvReleaseImage(h_plane);
|
||
cvReleaseImage(s_plane);
|
||
cvReleaseImage(v_plane);
|
||
cvReleaseImage(_h_plane);
|
||
cvReleaseImage(_s_plane);
|
||
cvReleaseImage(_v_plane);
|
||
cvReleaseImage(lut0);
|
||
cvReleaseImage(lut1);
|
||
cvReleaseImage(lut2);
|
||
end;
|
||
end;
|
||
|
||
// ------------------------------------ Öâåòîâàÿ òåìïåðàòóðà ------------------------------
|
||
{
|
||
Èíãðåäèåíòû: òå æå, ÷òî è â îòòåíêå, íî ãèñòîãðàììû äëÿ êðàñíîãî è çåëåíîãî ïîëîæèòåëüíûå, à äëÿ ñèíåãî ñëîÿ äâîéíàÿ îòðèöàòåëüíàÿ.
|
||
Öâåòîâàÿ òåìïåðàòóðà õàðàêòåðèçóåò íàëè÷èå â èçîáðàæåíèè æåëòîãî è ñèíåãî öâåòîâ. Çíà÷èò áóäåì «êðóòèòü» ñèíèé.
|
||
}
|
||
|
||
procedure Temperature(const step: Integer; const InputImage: pIplImage; Var OutputImage: pIplImage);
|
||
Var
|
||
h_plane, s_plane, v_plane: pIplImage;
|
||
_h_plane, _s_plane, _v_plane: pIplImage;
|
||
lut0, lut1, lut2: pIplImage;
|
||
begin
|
||
h_plane := cvCreateImage(cvGetSize(InputImage), IPL_DEPTH_8U, 1);
|
||
s_plane := cvCreateImage(cvGetSize(InputImage), IPL_DEPTH_8U, 1);
|
||
v_plane := cvCreateImage(cvGetSize(InputImage), IPL_DEPTH_8U, 1);
|
||
_h_plane := cvClone(h_plane);
|
||
_s_plane := cvClone(s_plane);
|
||
_v_plane := cvClone(v_plane);
|
||
try
|
||
cvCvtPixToPlane(InputImage, h_plane, s_plane, v_plane, nil);
|
||
lut0 := GetGammaExpo(-step * 2);
|
||
lut1 := GetGammaExpo(step);
|
||
lut2 := GetGammaExpo(step);
|
||
cvLUT(h_plane, _h_plane, lut0);
|
||
cvLUT(s_plane, _s_plane, lut1);
|
||
cvLUT(v_plane, _v_plane, lut2);
|
||
OutputImage := cvCreateImage(cvGetSize(InputImage), InputImage^.depth, InputImage^.nChannels);
|
||
cvCvtPlaneToPix(_h_plane, _s_plane, _v_plane, nil, OutputImage);
|
||
finally
|
||
cvReleaseImage(h_plane);
|
||
cvReleaseImage(s_plane);
|
||
cvReleaseImage(v_plane);
|
||
cvReleaseImage(_h_plane);
|
||
cvReleaseImage(_s_plane);
|
||
cvReleaseImage(_v_plane);
|
||
cvReleaseImage(lut0);
|
||
cvReleaseImage(lut1);
|
||
cvReleaseImage(lut2);
|
||
end;
|
||
end;
|
||
|
||
// ------------------------------------ Ñâåò è òåíè ------------------------------
|
||
{
|
||
Ïàðàìåòð «ñâåò» õàðàêòåðèçóåò ÿðêîñòü ñâåòëûõ îáëàñòåé èçîáðàæåíèÿ,
|
||
à ïàðàìåòð «òåíè» — ÿðêîñòü òåìíûõ îáëàñòåé. Ïðåîáðàçîâûâàòü áóäåì êàíàë ÿðêîñòåé.
|
||
}
|
||
function GetGammaLightShadow(const step: Integer; const reverse: Boolean): pIplImage;
|
||
const
|
||
M_E = 2.71828182845905;
|
||
Var
|
||
i: Integer;
|
||
d: double;
|
||
begin
|
||
result := cvCreateImage(cvSize(256, 1), IPL_DEPTH_8U, 1);
|
||
if reverse then
|
||
d := 256
|
||
else
|
||
d := 0;
|
||
for i := 0 to 255 do
|
||
result^.imageData[i] := AddDoubleToByte(i, power(0.36811145 * M_E, -power(abs(d - i), 1.7)) * 0.2 * step * abs(d - i));
|
||
end;
|
||
|
||
procedure White(const step: Integer; const InputImage: pIplImage; Var OutputImage: pIplImage);
|
||
Var
|
||
h_plane: pIplImage;
|
||
s_plane: pIplImage;
|
||
v_plane: pIplImage;
|
||
lut: pIplImage;
|
||
_v_plane: pIplImage;
|
||
begin
|
||
ImageToHSVPlane(InputImage, h_plane, s_plane, v_plane);
|
||
_v_plane := cvClone(v_plane);
|
||
try
|
||
lut := GetGammaLightShadow(step, true);
|
||
cvLUT(v_plane, _v_plane, lut);
|
||
OutputImage := cvCreateImage(cvGetSize(InputImage), InputImage^.depth, InputImage^.nChannels);
|
||
HSVPlaneToImage(h_plane, s_plane, _v_plane, OutputImage);
|
||
finally
|
||
cvReleaseImage(h_plane);
|
||
cvReleaseImage(s_plane);
|
||
cvReleaseImage(v_plane);
|
||
cvReleaseImage(_v_plane);
|
||
cvReleaseImage(lut);
|
||
end;
|
||
end;
|
||
|
||
procedure Shadow(const step: Integer; const InputImage: pIplImage; Var OutputImage: pIplImage);
|
||
Var
|
||
h_plane: pIplImage;
|
||
s_plane: pIplImage;
|
||
v_plane: pIplImage;
|
||
lut: pIplImage;
|
||
_v_plane: pIplImage;
|
||
begin
|
||
ImageToHSVPlane(InputImage, h_plane, s_plane, v_plane);
|
||
_v_plane := cvClone(v_plane);
|
||
try
|
||
lut := GetGammaLightShadow(step, false);
|
||
cvLUT(v_plane, _v_plane, lut);
|
||
OutputImage := cvCreateImage(cvGetSize(InputImage), InputImage^.depth, InputImage^.nChannels);
|
||
HSVPlaneToImage(h_plane, s_plane, _v_plane, OutputImage);
|
||
finally
|
||
cvReleaseImage(h_plane);
|
||
cvReleaseImage(s_plane);
|
||
cvReleaseImage(v_plane);
|
||
cvReleaseImage(_v_plane);
|
||
cvReleaseImage(lut);
|
||
end;
|
||
end;
|
||
|
||
// -------------------------------- Êîíòðàñò ----------------------------------
|
||
{
|
||
Êîíòðàñò îïðåäåëÿåòñÿ â ðàçíîñòè ÿðêîñòåé.
|
||
Ò.å. äëÿ óâåëè÷åíèÿ êîíòðàñòà íàì íóæíî ðàçäâèíóòü äèàïàçîí
|
||
ÿðêîñòåé îò öåíòðà ê êðàÿì. Ïðåîáðàçîâàíèå âûïîëíÿåòñÿ äëÿ âñåõ ñëîåâ.
|
||
}
|
||
procedure Contrast(const step: Integer; const InputImage: pIplImage; Var OutputImage: pIplImage);
|
||
Var
|
||
rgb_r: pIplImage;
|
||
rgb_g: pIplImage;
|
||
rgb_b: pIplImage;
|
||
_rgb_r: pIplImage;
|
||
_rgb_g: pIplImage;
|
||
_rgb_b: pIplImage;
|
||
contrastLevel, d: double;
|
||
lut: pIplImage;
|
||
p: PByte;
|
||
i: Integer;
|
||
begin
|
||
rgb_r := cvCreateImage(cvGetSize(InputImage), IPL_DEPTH_8U, 1);
|
||
rgb_g := cvCreateImage(cvGetSize(InputImage), IPL_DEPTH_8U, 1);
|
||
rgb_b := cvCreateImage(cvGetSize(InputImage), IPL_DEPTH_8U, 1);
|
||
cvCvtPixToPlane(InputImage, rgb_r, rgb_g, rgb_b, nil);
|
||
_rgb_r := cvClone(rgb_r);
|
||
_rgb_g := cvClone(rgb_g);
|
||
_rgb_b := cvClone(rgb_b);
|
||
lut := cvCreateImage(cvSize(256, 1), IPL_DEPTH_8U, 1);
|
||
try
|
||
contrastLevel := (100 + step) / 100;
|
||
p := lut^.imageData;
|
||
for i := 0 to 255 do
|
||
begin
|
||
d := ((i / 255 - 0.5) * contrastLevel + 0.5) * 255;
|
||
if (d > 255) then
|
||
d := 255
|
||
else if (d < 0) then
|
||
d := 0;
|
||
p[i] := Trunc(d);
|
||
end;
|
||
cvLUT(rgb_r, _rgb_r, lut);
|
||
cvLUT(rgb_g, _rgb_g, lut);
|
||
cvLUT(rgb_b, _rgb_b, lut);
|
||
OutputImage := cvCreateImage(cvGetSize(InputImage), InputImage^.depth, InputImage^.nChannels);
|
||
cvCvtPlaneToPix(_rgb_r, _rgb_g, _rgb_b, nil, OutputImage);
|
||
finally
|
||
cvReleaseImage(rgb_r);
|
||
cvReleaseImage(rgb_g);
|
||
cvReleaseImage(rgb_b);
|
||
cvReleaseImage(_rgb_r);
|
||
cvReleaseImage(_rgb_g);
|
||
cvReleaseImage(_rgb_b);
|
||
cvReleaseImage(lut);
|
||
end;
|
||
end;
|
||
// -------------------------------- Ðåçêîñòü ----------------------------------
|
||
{
|
||
Ðåçêîñòü (÷åòêîñòü) îïðåäåëÿåòñÿ âûäåëåíèåì îòäåëüíûõ ýëåìåíòîâ, èõ êîíòóðîâ.
|
||
Âåëè÷èíà, îáðàòíàÿ ðåçêîñòè – ðàçìûòîñòü.
|
||
 opencv äëÿ ðàçìûòèÿ èçîáðàæåíèÿ èñïîëüçóåì ôóíêöèþ blur,
|
||
ïðèíèìàþùóþ â êà÷åñòâå ïàðàìåòðîâ èñõîäíîå èçîáðàæåíèå, âûõîäíîå èçîáðàæåíèå,
|
||
è ðàçìåð ìàòðèöû ðàçìûòèÿ. Îò ðàçìåðà ìàòðèöû ðàçìûòèÿ è çàâèñèò ñèëà ðàçìûòèÿ.
|
||
Ýòîò ðàçìåð äîëæåí áûòü ÷åòíûì, ÷òîáû íå óêàçûâàòü âðó÷íóþ öåíòð ìàòðèöû.
|
||
×åòêîñòü â opencv ïðîùå âñåãî ïîâûñèòü ñ ïîìîùüþ ìàòðèöû ñâåðòêè, èñïîëüçóÿ
|
||
ñïåöèàëüíóþ äëÿ ýòîãî ìàòðèöó. Ôóíêöèÿ «filter2D», êîòîðàÿ ïðèíèìàåò èñõîäíîå
|
||
èçîáðàæåíèå, ðåçóëüòèðóþùåå èçîáðàæåíèå, êîëè÷åñòâî áèò íà çíà÷åíèå ìàòðèöû
|
||
ñâåðòêè, ìàòðèöó ñâåðòêè, âûïîëíÿåò íåïîñðåäñòâåííî ïðåîáðàçîâàíèå.
|
||
Èòàê, êàê áóäåò âûãëÿäåòü ìåòîä ïîâûøåíèÿ/ïîíèæåíèÿ ÷åòêîñòè.
|
||
}
|
||
|
||
procedure Clarity(const step: Integer; const InputImage: pIplImage; Var OutputImage: pIplImage);
|
||
var
|
||
// matr: array [0 .. 9] of single;
|
||
kernel_matrix: pCvMat;
|
||
begin
|
||
OutputImage := cvCreateImage(cvGetSize(InputImage), InputImage^.depth, InputImage^.nChannels);
|
||
if (step < 0) then
|
||
cvSmooth(InputImage, OutputImage, cv_blur, -step * 2 + 1, step * 2 + 1, 0, 0)
|
||
else
|
||
begin
|
||
kernel_matrix := cvCreateMat(3, 3, CV_32FC1);
|
||
PSingle(kernel_matrix^.Data)[0] := -0.0375 - 0.05 * step;
|
||
PSingle(kernel_matrix^.Data)[1] := -0.0375 - 0.05 * step;
|
||
PSingle(kernel_matrix^.Data)[2] := -0.0375 - 0.05 * step;
|
||
PSingle(kernel_matrix^.Data)[3] := -0.0375 - 0.05 * step;
|
||
PSingle(kernel_matrix^.Data)[4] := 1.3 + 0.4 * step;
|
||
PSingle(kernel_matrix^.Data)[5] := -0.0375 - 0.05 * step;
|
||
PSingle(kernel_matrix^.Data)[6] := -0.0375 - 0.05 * step;
|
||
PSingle(kernel_matrix^.Data)[7] := -0.0375 - 0.05 * step;
|
||
PSingle(kernel_matrix^.Data)[8] := -0.0375 - 0.05 * step;
|
||
cvFilter2D(InputImage, OutputImage, kernel_matrix);
|
||
end;
|
||
end;
|
||
|
||
end.
|