Laex / Delphi-OpenCV

Project Delphi-OpenCV. Translation of OpenCV library header files in Delphi
508 stars 227 forks source link

function cvImage2Bitmap() didn't work, i wrote a new one #134

Open OllinuxD opened 4 years ago

OllinuxD commented 4 years ago

function cvImage2Bitmap didn't work for me, it produced an exception.

this code instead works:

function cvImage2Bitmap(img: PIplImage): {$IFDEF DELPHIXE2_UP}Vcl.Graphics.TBitmap{$ELSE}Graphics.TBitmap{$ENDIF}; var bmp: {$IFDEF DELPHIXE2_UP}Vcl.Graphics.TBitmap{$ELSE}Graphics.TBitmap{$ENDIF}; deep: Integer; y, x, c, wStep, Channels: Integer; srcPtr: PByte; destPtr: PByte; wAdd: Integer; begin Result := nil; if (img <> nil) then begin bmp := {$IFDEF DELPHIXE2_UP}Vcl.Graphics.TBitmap.Create{$ELSE}Graphics.TBitmap.Create{$ENDIF}; bmp.SetSize(img^.Width, img^.Height); deep := img^.nChannels * img^.depth; case deep of 8: bmp.PixelFormat := pf8bit; 16: bmp.PixelFormat := pf16bit; 24: bmp.PixelFormat := pf24bit; 32: bmp.PixelFormat := pf32bit; end; wStep := img^.WidthStep; Channels := img^.nChannels; srcPtr := Pointer(img^.ImageData); wAdd := wStep - (img^.width * Channels); for y := 0 to img^.Height - 1 do begin destPtr := bmp.Scanline[y]; for x := 0 to img^.Width - 1 do begin for c := 0 to Channels - 1 do begin destPtr^ := srcPtr^; Inc(destPtr); Inc(srcPtr); end; end; srcPtr := Pointer(NativeUint(srcPtr) + NativeUInt(wAdd)); end; Result := bmp; end; end;

Laex commented 3 years ago

How can I reproduce the error? Which version of Delphi? More details ...

OllinuxD commented 3 years ago

Hello Laentir,

sorry, i can't give you more details. It's 10 month ago, i really can't remember. The original cvImage2Bitmap produced an exception. My Delphi-Version is always up to date, i suppose it was 10.3.3 Rio or maybe 10.4 Sydney.

Best regards,

Oliver

HuguesDug commented 2 years ago

Same on my side.

Sample of code that create the error : Don't get confused with the TMemoryStream. I use in FMX and not VCL. The two verion of bitmap are not directly compatible.

type TVclBitmap = Vcl.Graphics.TBitmap; var AVCLbitmap: TVclBitmap; AStream: TMemoryStream; Frame:PIplImage; begin if Capture <> nil then begin image := cvQueryFrame(Capture); if image <> nil then begin Frame:=cvCloneImage(image); AStream := TMemoryStream.Create; try AVCLbitmap := cvImage2Bitmap(Frame); AVCLbitmap.SaveToStream(AStream); AStream.Position := 0; CameraInput.Bitmap.LoadFromStream(AStream); finally AStream.free; AVCLbitmap.free; CvReleaseImage(Frame); end; end; end; end;

Environnement : Delphi 10.4.2, windows 32 plateform.

It seams it has to see with the line pb[3 j + K] := data[i wStep + j * Channels + K]

I commented out, no more error... but of course, no more image translation.

HuguesDug commented 2 years ago

function cvImage2Bitmap didn't work for me, it produced an exception.

this code instead works:

function cvImage2Bitmap(img: PIplImage): {$IFDEF DELPHIXE2_UP}Vcl.Graphics.TBitmap{$ELSE}Graphics.TBitmap{$ENDIF}; var bmp: {$IFDEF DELPHIXE2_UP}Vcl.Graphics.TBitmap{$ELSE}Graphics.TBitmap{$ENDIF}; deep: Integer; y, x, c, wStep, Channels: Integer; srcPtr: PByte; destPtr: PByte; wAdd: Integer; begin Result := nil; if (img <> nil) then begin bmp := {$IFDEF DELPHIXE2_UP}Vcl.Graphics.TBitmap.Create{$ELSE}Graphics.TBitmap.Create{$ENDIF}; bmp.SetSize(img^.Width, img^.Height); deep := img^.nChannels * img^.depth; case deep of 8: bmp.PixelFormat := pf8bit; 16: bmp.PixelFormat := pf16bit; 24: bmp.PixelFormat := pf24bit; 32: bmp.PixelFormat := pf32bit; end; wStep := img^.WidthStep; Channels := img^.nChannels; srcPtr := Pointer(img^.ImageData); wAdd := wStep - (img^.width * Channels); for y := 0 to img^.Height - 1 do begin destPtr := bmp.Scanline[y]; for x := 0 to img^.Width - 1 do begin for c := 0 to Channels - 1 do begin destPtr^ := srcPtr^; Inc(destPtr); Inc(srcPtr); end; end; srcPtr := Pointer(NativeUint(srcPtr) + NativeUInt(wAdd)); end; Result := bmp; end; end;

Works for me... except I have to remove the IfDef and else, as the ifdef clause is not recognized by 10.4... for a reason I don't know.

sun5152 commented 10 months ago

Array out range

PByteArray = ^TByteArray; TByteArray = array[0..32767] of Byte;

function cvImage2Bitmap(img: PIplImage): {$IFDEF DELPHIXE2_UP}Vcl.Graphics.TBitmap{$ELSE}Graphics.TBitmap{$ENDIF}; var // info: string; bmp: {$IFDEF DELPHIXE2_UP}Vcl.Graphics.TBitmap{$ELSE}Graphics.TBitmap{$ENDIF}; deep: Integer; i, j, K, wStep, Channels: Integer; // data: PByteArray; // pb: PByteArray; pb,pData: Pointer; offset:longint; begin Result := NIL; if (img <> NIL) then begin bmp := {$IFDEF DELPHIXE2_UP}Vcl.Graphics.TBitmap.Create{$ELSE}Graphics.TBitmap.Create{$ENDIF}; bmp.Width := img^.Width; bmp.Height := img^.Height; deep := img^.nChannels * img^.depth; case deep of 8: bmp.PixelFormat := pf8bit; 16: bmp.PixelFormat := pf16bit; 24: bmp.PixelFormat := pf24bit; 32: bmp.PixelFormat := pf32bit; End;

wStep := img^.WidthStep;
Channels := img^.nChannels;

// data := Pointer(img^.ImageData);

for i := 0 to img^.Height - 1 do
begin
  pb := bmp.Scanline[i];
  offset := longint(img^.ImageData) + wStep * i;
  pData := Pointer(offset);
  copymemory(pb, pData, wStep);

// for j := 0 to img^.Width - 1 do // begin // for K := 0 to Channels - 1 do // pb[Channels j + K] := data[i wStep + j * Channels + K]; // End; End; Result := bmp; // bmp.Free; End; end;

OllinuxD commented 10 months ago

Vielen Dank für Ihre E-Mail.

Frohe Weihnachten und einen guten Rutsch ins neue Jahr!

In der Zeit vom 22.12.2023 - 03.01.2024 bin ich im Urlaub.

Ihr Anliegen wird in dieser Zeit nicht bearbeitet.

In dringenden Fällen wenden Sie sich bitte telefonisch an die 040 - 6737020.

Mit freundlichen Grüßen

Oliver Dahlmann, Dipl. Ing. (FH)


Oliver Dahlmann, Dipl. Ing. (FH) Ernst Sicherheits- und Kommunikationstechnik GmbH Eiffestraße 74 20537 Hamburg

Fax 040 - 673702-5030 Email @.*** Web http://www.ernst-sicherheitstechnik.de

Geschäftsführer: Arne Ernst Gerichtsstand Hamburg Zuständige Handelskammer: Handelskammer Hamburg Handelsregister: HRB23339 Steuer-Nr. 46/718/03340

array out range PByteArray = ^TByteArray; TByteArray = array[0..32767] of Byte;

-- Reply to this email directly or view it on GitHub: https://github.com/Laex/Delphi-OpenCV/issues/134#issuecomment-1874820547 You are receiving this because you authored the thread.

Message ID: @.***>