delphi - Reading Pixels and finding specific RGB -
i load image, , read pixels find rgb, check next pixels across make sure match, , @ right position of bitmap.
i know below code wrong, not sure how go correcting it. know pixels not fastest way read pixels.
thanks guys!
procedure rgb(col: tcolor; var r, g, b: byte); var color: $0..$ffffffff; begin color := colortorgb(col); r := ($000000ff , color); g := ($0000ff00 , color) shr 8; b := ($00ff0000 , color) shr 16; end; procedure tform1.button1click(sender: tobject); var x,y : integer; colorn: tcolor; r, g, b: byte; begin y := 0 image1.picture.bitmap.height -1 begin x := 0 image1.picture.bitmap.width -1 begin inc(i); colorn := image1.canvas.pixels[x, y]; rgb(colorn, r, g, b); //memo1.lines.append('line: '+inttostr(i)+' y: '+inttostr(y)+' x: '+inttostr(x)+' r: '+inttostr(r)+' g: '+inttostr(g)+' b: '+inttostr(b)); if (inttostr(r) = '235') , (inttostr(g) = '235') , (inttostr(b) = '235') //y: 500 x: 587 begin //image1.canvas.moveto(x,y); //image1.canvas.lineto(x,y); colorn := image1.canvas.pixels[x +1, y]; rgb(colorn, r, g, b); end; if (inttostr(r) = '232') , (inttostr(g) = '232') , (inttostr(b) = '232') //rgb:232,232,232 y: 500 x: 588 begin colorn := image1.canvas.pixels[x +1, y]; rgb(colorn, r, g, b); showmessage('test1'); end; if (inttostr(r) = '231') , (inttostr(g) = '231') , (inttostr(b) = '231') //rgb: 231,231,231 y: 500 x: 589 begin colorn := image1.canvas.pixels[x +1, y]; rgb(colorn, r, g, b); showmessage('test2'); end; if (inttostr(r) = '230') , (inttostr(g) = '230') , (inttostr(b) = '230') //rgb: 230,230,230 y: 500 x: 590 begin showmessage('test3'); end; end; end; end; procedure tform1.formcreate(sender: tobject); var b: tbitmap; begin image1.picture.loadfromfile('e:\delphi projects\detect(xe6)\screen\1.png'); b := tbitmap.create; b.assign(image1.picture.graphic); image1.picture.bitmap := b; freeandnil(b); end;
there several big issues in code:
- instead of
inttostr(r) = '235'
faster checkr=235
- instead of checking r, g , b individually, easier check
if colorn=c_gray235
c_gray235 = #00ebebeb;
for application whole i'd use single if:
if (getpixel(x, y) = c_gray235) , (getpixel(x+1, y) = c_gray232) , (getpixel(x+2, y) = c_gray231) , (getpixel(x+3, y) = c_gray230) begin //do stuff here end;
please note for-loop should for x := 0 mybitmap.width - 4
(there's no way if succeed once have less 4 pixels left on line. may av if try access them, depending on way pixels).
now if bitmap 24-bit or 32-bit bitmap, can improve performance quite bit using bitmap.scanline[iline]...
Comments
Post a Comment