what is code for
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
// In this version, we try to find the best orientation by rotating the
// rectangle drawn by the user
// Bounding rectangle is defined by the line along the IQI axis
// In this version, the rectangle completely covers the IQI
function BasicSpacialResolutionAnalysis2(
const Prm : PIQIQualificationRec2) : Integer;
var
// Theta : Double;
Method : TIQIStandard;
AngleDelta : Extended;
X1, XX1 : Integer;
Y1, YY1 : Integer;
X2, XX2 : Integer;
Y2, YY2 : Integer;
X3, XX3 : Integer;
Y3, YY3 : Integer;
Slope : Extended;
NBand : Integer;
I, J : Integer;
DX, DY : Extended;
X : Integer;
Stream : TFileStream;
FileName : String;
BestValue : Integer;
BestIndex : Integer;
BestOfBest : Integer;
BestSlope : Extended;
BestX1, BestY1, BestX2, BestY2, BestX3, BestY3 : Integer;
BestArray : TArray;
Buf : AnsiString;
BestValueMean : Integer;
BestValueIndex : Integer;
Line1 : TWordDynArray;
Line2 : TWordDynArray;
CsvFile : String;
TryNumber : Integer;
MinX, MaxX : Integer;
begin
if Prm.CbSize = SizeOf(TIQIQualificationRec) then begin
Result := BasicSpacialResolutionAnalysis(PIQIQualificationRec(Prm));
Exit;
end;
Result := CheckPrm2(Prm);
if Result <> ERROR_IQI_NO_ERROR then
Exit;
// Set the qualification timestamp
Prm.DateTime := Now;
if StrComp(Prm.Standard, 'IQI ' + IQI_ISO_17636_2_2010_STR) = 0 then
Method := IQI_ISO_17636_2_20120
else if StrComp(Prm.Standard, 'IQI ' + IQI_ASTM_E_2597_07_STR) = 0 then
Method := IQI_ASTM_E_2597_07
else
Method := IQI_UNSUPPORTED;
// Currently we support only ISO 17636-2:2010
if Method <> IQI_ISO_17636_2_20120 then begin
Result := ERROR_IQI_UNSUPPORTED_STANDARD;
Exit;
end;
FileName := Prm.ReportPath + 'IQIProfile' +
FormatDateTime('_YYYYMMDD_HHNNSS', Prm.DateTime) + '.log';
DeleteFile(FileName);
Stream := TFileStream.Create(FileName, fmCreate);
try
for TryNumber := 0 to 3 do begin
if TryNumber = 1 then begin
// Swap point 1 and point 2
SwapInteger(Prm.X1,Prm.X2);
SwapInteger(Prm.Y1,Prm.Y2);
// Choose other 3rd point
Prm.X3 := Prm.X3 + (Prm.X2 - Prm.X1);
Prm.Y3 := Prm.Y3 + (Prm.Y2 - Prm.Y1);
end;
if TryNumber = 2 then begin
// Choose other 2nd point
Prm.X2 := Prm.X1 + (Prm.X3 - Prm.X2);
Prm.Y2 := Prm.Y1 + (Prm.Y3 - Prm.Y2);
end;
if TryNumber = 3 then begin
// Swap point 1 and point 2
SwapInteger(Prm.X1,Prm.X2);
SwapInteger(Prm.Y1,Prm.Y2);
// Choose other 3rd point
Prm.X3 := Prm.X3 + (Prm.X2 - Prm.X1);
Prm.Y3 := Prm.Y3 + (Prm.Y2 - Prm.Y1);
end;
(*
// The line (X1,Y1))(X2,Y2) is along the IQI, X1 near the largest pair
if Prm.X2 = Prm.X1 then
Theta := 90
else
Theta := Abs(ArcTan((Prm.Y2 - Prm.Y1) / (Prm.X2 - Prm.X1)) * 360 / 2 / PI);
if not (
((Theta >= 2) and (Theta <= 5)) or
((Theta >= 85) and (Theta <= 88))
) then
Prm.MessageBox(PChar('WARNING: Incorrect slope (' +
Format('%3.1f', [Abs(Theta)]) +
'. It should be between 2 and 5 degrees'),
'IQI analysis warning');
if SQRT(Sqr(Prm.X3 - Prm.X2) + Sqr(Prm.Y3 - Prm.Y2)) < 21 then
Prm.MessageBox('WARNING: Area should cover at least ' +
'21 linear profiles on the IQI',
'IQI analysis warning');
*)
(*
// Standard says that mean grey level should be at least 50% of max grey
if Prm.MaxGrey = 0 then
Prm.MessageBox('WARNING: Image file format may be incompatible',
'IQI analysis warning')
else if Prm.Mean < (Prm.MaxGrey div 2) then
Prm.MessageBox(PChar('WARNING: Mean grey level is too low (' +
IntToStr(Prm.Mean) +
'). Should be at least 50% of maximum level ' +
'(' + IntToStr(Prm.MaxGrey) + ')'),
'IQI analysis warning');
*)
// Compute the angle corresponding to one pixel
if Abs(Prm.X2 - Prm.X1) > Abs(Prm.Y2 - Prm.Y1) then
// More horizontal than vertical
AngleDelta := ArcTan2(1, Prm.X2 - Prm.X1)
else
AngleDelta := ArcTan2(1, Prm.Y2 - Prm.Y1);
Buf := AnsiString('Selected area ' +
IntToStr(Prm.X1) + ' ' +
IntToStr(Prm.Y1) + ' ' +
IntToStr(Prm.X2) + ' ' +
IntToStr(Prm.Y2) + ' ' +
IntToStr(Prm.X3) + ' ' +
IntToStr(Prm.Y3) + #13#10);
Stream.Write(Buf[1], Length(Buf));
XX1 := Prm.X1;
YY1 := Prm.Y1;
XX2 := Prm.X2;
YY2 := Prm.Y2;
XX3 := Prm.X3;
YY3 := Prm.Y3;
// Search best slope using a rather large band
BestOfBest := 0;
BestSlope := 0;
//Slope := (YY3 - YY2) / (XX3 - XX2);
Slope := IfThen(XX3 = XX2, 1, (YY3 - YY2) / (XX3 - XX2));
NBand := 5;
DX := (XX3 - XX2) / NBand;
DY := DX * Slope;
for I := 0 to NBand - 1 do begin
X1 := XX1 + Round(I * DX);
Y1 := YY1 + Round(I * DY);
X2 := XX2 + Round(I * DX);
Y2 := YY2 + Round(I * DY);
X3 := XX2 + Round((I + 1) * DX);
Y3 := YY2 + Round((I + 1) * DY);
BasicSpacialResolutionAnalysis2Compute(
Prm,
X1, Y1, X2, Y2, X3, Y3,
BestIndex, BestValue,
useRawData,
AngleDelta, 40,
Stream);
if BestValue > BestOfBest then begin
BestOfBest := BestValue;
//BestSlope := (Y2 - Y1) / (X2 - X1);
BestSlope := IfThen(X2 = X1, 1, (Y2 - Y1) / (X2 - X1));
BestX1 := X1;
BestY1 := Y1;
BestX2 := X2;
BestY2 := Y2;
BestX3 := X3;
BestY3 := Y3;
end;
end;
if Prm.IQIIndex >= 3 then
Break;
end;
if Prm.IQIIndex < 3 then begin
Prm.MessageBox(PChar('IQI Duplex not detected'),
'IQI analysis warning');
Result := ERROR_IQI_INVALID_ORIENTATION;
Exit;
end;
// Now that we have the best slope, fix initial orientation by
// rotating around (X2, Y2)
XX1 := Round(Prm.X2 - (Prm.Y2 - Prm.Y1) / BestSlope);
YY1 := Prm.Y1;
XX2 := Prm.X2;
YY2 := Prm.Y2;
XX3 := Prm.X3;
YY3 := Round(Prm.Y2 + (Prm.X2 - Prm.X3) / BestSlope);
Buf := AnsiString('..................' + #13#10);
Stream.Write(Buf[1], Length(Buf));
if XX2 < XX3 then begin
MinX := XX2;
MaxX := XX3;
end
else begin
MinX := XX3;
MaxX := XX2;
end;
SetLength(BestArray, MaxX - MinX + 1);
for X := MinX to MaxX do begin
X2 := X;
Y2 := Round(YY2 + (XX2 - X) / BestSlope);
X3 := X2 + 1;
Y3 := Y2;
X1 := XX1 + (X2 - XX2);
Y1 := YY1 + (Y2 - YY2);
BasicSpacialResolutionAnalysis2Compute(
Prm,
X1, Y1, X2, Y2, X3, Y3,
BestIndex, BestValue,
useRawData,
AngleDelta, 0,
Stream);
BestArray[X - MinX].X1 := X1;
BestArray[X - MinX].Y1 := Y1;
BestArray[X - MinX].X2 := X2;
BestArray[X - MinX].Y2 := Y2;
BestArray[X - MinX].X3 := X3;
BestArray[X - MinX].Y3 := Y3;
BestArray[X - MinX].Value := BestValue;
end;
// Log unsorted BestArray
Buf := AnsiString('......... UNSORTED .........' + #13#10);
Stream.Write(Buf[1], Length(Buf));
for I := 0 to High(BestArray) do begin
Buf := AnsiString(IntToStr(I + 1) + ') ' +
IntToStr(BestArray[I].X1) + ' ' +
IntToStr(BestArray[I].Y1) + ' ' +
IntToStr(BestArray[I].X2) + ' ' +
IntToStr(BestArray[I].Y2) + ' ' +
IntToStr(BestArray[I].X3) + ' ' +
IntToStr(BestArray[I].Y3) + ' -> ' +
IntToStr(BestArray[I].Value) + #13#10);
Stream.Write(Buf[1], Length(Buf));
end;
// Using a mobile mean, search the area where we have a max for 21 lines
// We may have BestArray length smaller than 21
BestValueMean := -1;
BestValueIndex := -1;
J := 0;
for I := 0 to Max(0, High(BestArray) - 20) do begin
X := 0;
J := 0;
repeat
Inc(X, BestArray[I + J].Value);
Inc(J);
until (J >= 21) or ((I + J) > High(BestArray));
X := X div J;
if X > BestValueMean then begin
BestValueMean := X;
BestValueIndex := I;
end;
end;
// Now we have best area
XX1 := BestArray[BestValueIndex].X1;
YY1 := BestArray[BestValueIndex].Y1;
XX2 := BestArray[BestValueIndex].X2;
YY2 := BestArray[BestValueIndex].Y2;
XX3 := BestArray[BestValueIndex + J - 1].X3;
YY3 := BestArray[BestValueIndex + J - 1].Y3;
Buf := AnsiString('......... BEST AREA .........' + #13#10);
Stream.Write(Buf[1], Length(Buf));
Buf := AnsiString(IntToStr(BestValueIndex + 1) + ') ' +
IntToStr(XX1) + ' ' +
IntToStr(YY1) + ' ' +
IntToStr(XX2) + ' ' +
IntToStr(YY2) + ' ' +
IntToStr(XX3) + ' ' +
IntToStr(YY3) + #13#10);
Stream.Write(Buf[1], Length(Buf));
// Compute exactly BestArea
BasicSpacialResolutionAnalysis2Compute(
Prm,
XX1, YY1, XX2, YY2, XX3, YY3,
BestIndex, BestValue,
useRawData,
AngleDelta, 0,
Stream);
// Returns final area to the caller
Prm.OutX1 := XX1;
Prm.OutY1 := YY1;
Prm.OutX2 := XX2;
Prm.OutY2 := YY2;
Prm.OutX3 := XX3;
Prm.OutY3 := YY3;
CsvFile := Prm.ReportPath + 'IQIProfile' +
FormatDateTime('_YYYYMMDD_HHNNSS', Prm.DateTime) + '.csv';
Prm.IQIVisible := Prm.IQIIndex;
Line1 := ProfileLineFromProfileRectangle(Prm.FrameBufferRaw,
Prm.FrameWidth,
Prm.FrameHeight,
Prm.BytesPerPixel,
Prm.BitsPerPixel,
XX1,
YY1,
XX2,
YY2,
XX3,
YY3);
if Assigned(Prm.FrameBufferFiltered) then begin
// Get the pixels from filtered bitmap
Line2 := ProfileLineFromProfileRectangle(Prm.FrameBufferFiltered,
Prm.FrameWidth,
Prm.FrameHeight,
Prm.BytesPerPixel,
Prm.BitsPerPixel,
XX1,
YY1,
XX2,
YY2,
XX3,
YY3);
SearchVisiblePairOnFilteredImage(Prm, Line2, Stream);
SaveWordsToFile(System.PWord(Line1), System.PWord(Line2),
Length(Line1), CsvFile);
end
else
SaveWordsToFile(System.PWord(Line1), nil, Length(Line1), CsvFile);
(*
// Sort BestArray so that the best is at index 0
TArray.Sort(BestArray, TDelegatedComparer.Construct(
function(const Left, Right : TBestRecord) : Integer
begin
Result := -TComparer.Default.Compare(Left.Value, Right.Value);
end));
// Log sorted BestArray
Buf := AnsiString('......... SORTED .........' + #13#10);
Stream.Write(Buf[1], Length(Buf));
for I := Low(BestArray) to High(BestArray) do begin
Buf := AnsiString(IntToStr(I + 1) + ') ' +
IntToStr(BestArray[I].X1) + ' ' +
IntToStr(BestArray[I].Y1) + ' ' +
IntToStr(BestArray[I].X2) + ' ' +
IntToStr(BestArray[I].Y2) + ' ' +
IntToStr(BestArray[I].X3) + ' ' +
IntToStr(BestArray[I].Y3) + ' -> ' +
IntToStr(BestArray[I].Value) + #13#10);
Stream.Write(Buf[1], Length(Buf));
end;
*)
finally
FreeAndNil(Stream);
end;
end;
function BasicSpacialResolutionAnalysis2Compute(
const Prm : PIQIQualificationRec2;
var XX1, YY1, XX2, YY2, XX3, YY3 : Integer;
var BestIndex : Integer;
var BestValue : Integer;
AUseRawData : TFrameType;
AngleDelta : Extended;
NWidth : Integer; // Number of increments on each side
Stream : TFileStream) : Integer;
var
PrmTmp : array of TPrmSet;
I, J : Integer;
Line : TWordDynArray;
AMean : Word;
AStdDev : Word;
AMin : Word;
AMax : Word;
Angle : Extended;
X1 : Integer;
Y1 : Integer;
X3 : Integer;
Y3 : Integer;
Value : Integer;
Buf : AnsiString;
FrameBuffer : Pointer;
//{IF MAX_PAIR_COUNT <> 13}
// RoiLenMM : Double; // Roi length in millimeter
//{ENDIF}
begin
Result := ERROR_IQI_NO_ERROR;
BestValue := 0;
BestIndex := 0;
// Set the qualification timestamp
Prm.DateTime := Now;
// In main program, autoclose the window used for IQI qualification
Prm.AutoClose := TRUE;
Angle := -(NWidth + 1) * AngleDelta;
// Create a set of parameters
SetLength(PrmTmp, NWidth + NWidth + 1);
for J := 0 to High(PrmTmp) do begin
// We need independant PairDef
for I := Low(PrmTmp[J].PairDef) to High(PrmTmp[J].PairDef) do
PrmTmp[J].PairDef[I].CbSize := SizeOf(PrmTmp[J].PairDef[I]);
// Rotate (X1, Y1) and (X3, Y3) around (X2, Y2)
Angle := Angle + AngleDelta;
RotatePoint(XX1, YY1, XX2, YY2, Angle, X1, Y1);
RotatePoint(XX3, YY3, XX2, YY2, Angle, X3, Y3);
PrmTmp[J].Prm.X1 := X1;
PrmTmp[J].Prm.Y1 := Y1;
PrmTmp[J].Prm.X2 := XX2;
PrmTmp[J].Prm.Y2 := YY2;
PrmTmp[J].Prm.X3 := X3;
PrmTmp[J].Prm.Y3 := Y3;
if AUseRawData = AUseRawData then
FrameBuffer := Prm.FrameBufferRaw
else
FrameBuffer := Prm.FrameBufferFiltered;
Line := ProfileLineFromProfileRectangle(FrameBuffer,
Prm.FrameWidth,
Prm.FrameHeight,
Prm.BytesPerPixel,
Prm.BitsPerPixel,
PrmTmp[J].Prm.X1,
PrmTmp[J].Prm.Y1,
PrmTmp[J].Prm.X2,
PrmTmp[J].Prm.Y2,
PrmTmp[J].Prm.X3,
PrmTmp[J].Prm.Y3);
//SaveWordsToFile(@Line[0], nil, Length(Line),
// Format('E:\PhantomData\Reports\MTFPointsMean_%02.2d.csv', [J]));
SetLength(PrmTmp[J].Points, Length(Line));
SetLength(PrmTmp[J].PointsMean, Length(Line));
for I := 0 to High(Line) do
PrmTmp[J].Points[I] := Line[I];
PrmTmp[J].Prm.CbSize := SizeOf(PrmTmp[J].Prm);
PrmTmp[J].Prm.PairDef := @(PrmTmp[J].PairDef);
PrmTmp[J].Prm.IQIIndex := @(PrmTmp[J].IQIIndex);
PrmTmp[J].Prm.Points := @(PrmTmp[J].Points[0]);
PrmTmp[J].Prm.PointsArray := @(PrmTmp[J].Points);
PrmTmp[J].Prm.PointsMean := @(PrmTmp[J].PointsMean[0]);
PrmTmp[J].Prm.PointsMeanArray := @(PrmTmp[J].PointsMean);
PrmTmp[J].Prm.PointsCount := Length(PrmTmp[J].Points);
MeanAndStdDevBndt(PrmTmp[J].Points, AMean, AStdDev);
MinAndMax(PrmTmp[J].Points, AMin, AMax);
PrmTmp[J].Prm.Max := AMax;
PrmTmp[J].Prm.Min := AMin;
PrmTmp[J].Prm.Mean := AMean;
PrmTmp[J].Prm.StdDev := AStdDev;
PrmTmp[J].Prm.MaxGrey := Prm.MaxGrey;
PrmTmp[J].Prm.Standard := Prm.Standard;
PrmTmp[J].Prm.ProgVer := Prm.ProgVer;
PrmTmp[J].Prm.ProgName := Prm.ProgName;
PrmTmp[J].Prm.DetectorSN := Prm.DetectorSN;
PrmTmp[J].Prm.CustomerName := Prm.CustomerName;
PrmTmp[J].Prm.LicenceID := Prm.LicenceID;
PrmTmp[J].Prm.MessageBox := Prm.MessageBox;
PrmTmp[J].Prm.ParentHandle := Prm.ParentHandle;
PrmTmp[J].Prm.PluginData := Prm.PluginData;
PrmTmp[J].Prm.ReportPath := Prm.ReportPath;
PrmTmp[J].Prm.BitMapFile := Prm.BitmapRaw.BitMapFile;
PrmTmp[J].Prm.BitMapWidth := Prm.BitmapRaw.BitMapWidth;
PrmTmp[J].Prm.BitMapHeight := Prm.BitmapRaw.BitMapHeight;
PrmTmp[J].Prm.DateTime := Prm.DateTime;
PrmTmp[J].Prm.AutoClose := Prm.AutoClose;
PrmTmp[J].Prm.NoShowPDF := Prm.NoShowPDF;
PrmTmp[J].Prm.PdfFileName := Prm.PdfFileName;
PrmTmp[J].Prm.BytesPerPixel := Prm.BytesPerPixel;
PrmTmp[J].Prm.BitsPerPixel := Prm.BitsPerPixel;
PrmTmp[J].Prm.FrameBuffer := Prm.FrameBufferRaw;
PrmTmp[J].Prm.FrameWidth := Prm.FrameWidth;
PrmTmp[J].Prm.FrameHeight := Prm.FrameHeight;
PrmTmp[J].Res := BasicSpacialResolutionAnalysisCompute(@(PrmTmp[J].Prm), TRUE);
PrmTmp[J].SaveToStream(Stream);
end;
// Find best value
BestIndex := -1;
BestValue := 0;
for J := 0 to High(PrmTmp) do begin
if PrmTmp[J].Res <> ERROR_IQI_NO_ERROR then
continue;
Value := PrmTmp[J].Prm.IQIIndex^ * 100 +
PrmTmp[J].Prm.PairDef[PrmTmp[J].Prm.IQIIndex^].Modulation;
if Value > BestValue then begin
BestValue := Value;
BestIndex := J;
end;
end;
if BestIndex >= 0 then begin
// For debugging, recompute best position
PrmTmp[BestIndex].Res := BasicSpacialResolutionAnalysisCompute(@(PrmTmp[BestIndex].Prm), TRUE);
// Copy final optimal result to original parameters
Prm.IQIIndex := PrmTmp[BestIndex].Prm.IQIIndex^;
for I := Low(Prm.PairDef) to High(Prm.PairDef) do
Prm.PairDef[I] := PrmTmp[BestIndex].Prm.PairDef[I];
XX1 := PrmTmp[BestIndex].Prm.X1;
YY1 := PrmTmp[BestIndex].Prm.Y1;
XX2 := PrmTmp[BestIndex].Prm.X2;
YY2 := PrmTmp[BestIndex].Prm.Y2;
XX3 := PrmTmp[BestIndex].Prm.X3;
YY3 := PrmTmp[BestIndex].Prm.Y3;
ComputePairCount2(Prm);
//{IF MAX_PAIR_COUNT = 13}
// Prm.PairCount := 13;
//{ELSE}
// if Prm.IQIIndex <= 1 then
// Prm.PairCount := MAX_PAIR_COUNT
// else begin
// OneMillimeter := (Prm.PairDef[Prm.IQIIndex].IndexMin - Prm.PairDef[1].IndexMin) /
// (WirePairDist[Prm.IQIIndex] - WirePairDist[1]);
//
// // Find out how many line pairs there are in the ROI
// RoiLenMM := (Sqrt(Sqr(Prm.X2 - Prm.X1) +
// Sqr(Prm.Y2 - Prm.Y1)) -
// Prm.PairDef[1].IndexMin) / OneMillimeter;
// if RoiLenMM > WirePairDist[MAX_PAIR_COUNT] then
// Prm.PairCount := MAX_PAIR_COUNT
// else
// Prm.PairCount := 13;
// end;
//{$ENDIF}
if Assigned(Stream) then begin
Buf := AnsiString(IntToStr(XX1) + ' ' +
IntToStr(YY1) + ' ' +
IntToStr(XX2) + ' ' +
IntToStr(YY2) + ' ' +
IntToStr(XX3) + ' ' +
IntToStr(YY3) + ' -> ' +
IntToStr(BestIndex) + ' ' +
IntToStr(BestValue));
for I := 1 to Prm.PairCount do
Buf := Buf + ' ' + AnsiString(IntToStr(PrmTmp[BestIndex].Prm.PairDef[I].IndexCenter));
// IntToStr(PrmTmp[BestIndex].Prm.PairDef[1].IndexCenter) + ' ' +
// IntToStr(PrmTmp[BestIndex].Prm.PairDef[2].IndexCenter) + ' ' +
// IntToStr(PrmTmp[BestIndex].Prm.PairDef[3].IndexCenter) + ' ' +
// IntToStr(PrmTmp[BestIndex].Prm.PairDef[4].IndexCenter) + ' ' +
// IntToStr(PrmTmp[BestIndex].Prm.PairDef[5].IndexCenter) + ' ' +
// IntToStr(PrmTmp[BestIndex].Prm.PairDef[6].IndexCenter) + ' ' +
// IntToStr(PrmTmp[BestIndex].Prm.PairDef[7].IndexCenter) + ' ' +
// IntToStr(PrmTmp[BestIndex].Prm.PairDef[8].IndexCenter) + ' ' +
// IntToStr(PrmTmp[BestIndex].Prm.PairDef[9].IndexCenter) + ' ' +
// IntToStr(PrmTmp[BestIndex].Prm.PairDef[10].IndexCenter) + ' ' +
// IntToStr(PrmTmp[BestIndex].Prm.PairDef[11].IndexCenter) + ' ' +
// IntToStr(PrmTmp[BestIndex].Prm.PairDef[12].IndexCenter) + ' ' +
Buf := Buf + #13#10 + '====' + #13#10;
Stream.Write(Buf[1], Length(Buf));
end;
end;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}