How to know the attributes of a pixel in the image

This forum is for general support of Xbase++
Message
Author
User avatar
Eugene Lutsenko
Posts: 1649
Joined: Sat Feb 04, 2012 2:23 am
Location: Russia, Southern federal district, city of Krasnodar
Contact:

How to know the attributes of a pixel in the image

#1 Post by Eugene Lutsenko »

How to know the attributes of a pixel in the image. I want to upload an image and analyze it. How to upload an existing image is clear to me. And how to analyze it, I do not know. And I need it. It is advisable to use the standard graphical Alaska. Can anyone help?

User avatar
rdonnay
Site Admin
Posts: 4729
Joined: Wed Jan 27, 2010 6:58 pm
Location: Boise, Idaho USA
Contact:

Re: How to know the attributes of a pixel in the image

#2 Post by rdonnay »

If you know the format of a BMP file you can convert it to an array.

You would use FOpen() and FRead() to read the contents of the file and then create a 3-element array for each pixel (RGB).

Here is the format:

http://www.fileformat.info/format/bmp/egff.htm
The eXpress train is coming - and it has more cars.

User avatar
Eugene Lutsenko
Posts: 1649
Joined: Sat Feb 04, 2012 2:23 am
Location: Russia, Southern federal district, city of Krasnodar
Contact:

Re: How to know the attributes of a pixel in the image

#3 Post by Eugene Lutsenko »

rdonnay wrote:If you know the format of a BMP file you can convert it to an array.

You would use FOpen() and FRead() to read the contents of the file and then create a 3-element array for each pixel (RGB).

Here is the format:

http://www.fileformat.info/format/bmp/egff.htm
Hi, Roger!

This principle is clear. But only in principle. But in practice, I can not do it. Could you give a simple example program bmp-files of the type that is formed in the function: DC_Scrn2ImageFile(). Please...

In fact, I ask you to write a functions:
- aRGB = DC_GetFileRGB(cFile), bmp-file named: cFile, about which you wrote in your message;
- aRGBxy = DC_GetPointRGB(X, Y, aRGB), which returns the brightness beams R, G, B for the point with coordinates (X, Y)

User avatar
rdonnay
Site Admin
Posts: 4729
Joined: Wed Jan 27, 2010 6:58 pm
Location: Boise, Idaho USA
Contact:

Re: How to know the attributes of a pixel in the image

#4 Post by rdonnay »

After looking at the spec, I realize that this will be a lot of work due to the many different formats for image files.

The best approach now is to try to read the pixel value from the screen.

I will look into this.
The eXpress train is coming - and it has more cars.

User avatar
Eugene Lutsenko
Posts: 1649
Joined: Sat Feb 04, 2012 2:23 am
Location: Russia, Southern federal district, city of Krasnodar
Contact:

Re: How to know the attributes of a pixel in the image

#5 Post by Eugene Lutsenko »

rdonnay wrote:After looking at the spec, I realize that this will be a lot of work due to the many different formats for image files.

The best approach now is to try to read the pixel value from the screen.

I will look into this.
Thanks for the reply, Roger!

But it seems to me that your function DC_Scrn2ImageFile() only creates a well-defined type of bmp-file. So it turns out that it is sufficient to determine the type of bmp-file only once. And these functions, about which I wrote very much needed. It is strange that they are not in the basic graphics functions Alaska. Even in the graphics library, which I used in CLIPPER (it still fit for Pascal and C++) such functions were.

Or maybe really determine the pixel values are not in a graphic file, and immediately after the withdrawal of the graphic on the screen? It is a good idea that does not require or imply studies of the bmp-file. And if you want to explore the image file, then it is always possible to display, and then explore.

User avatar
rdonnay
Site Admin
Posts: 4729
Joined: Wed Jan 27, 2010 6:58 pm
Location: Boise, Idaho USA
Contact:

Re: How to know the attributes of a pixel in the image

#6 Post by rdonnay »

I tried using the windows api function GetPixel() but it keeps returning a -1.
This is the code. It shouldn't matter whether I load a .JPG or .BMP because they both are loaded by DC_GetBitMap() which uses the XbpBitmap() class. I have to leave for the day so I won't be able to debug this until later.

Maybe someone has an idea.
Compile it and run it.
Save the attached JPG to colors.jpg.

Code: Select all

#INCLUDE "dll.CH"
#INCLUDE "dcdialog.CH"

FUNCTION Main()

LOCAL GetList[0], oSay, nColor, cColor, nHDC

@ 0,0 DCSTATIC TYPE XBPSTATIC_TYPE_BITMAP ;
      CAPTION "colors.jpg" PREEVAL {|o|o:autoSize := .t.} ;
      EVAL {|o|nHDC := o:getHWND(), ;
               o:motion := {|a,b,o|ShowColor( nHDC, a, oSay )}}

@ 3,0 DCSAY '' SAYSIZE 20 FONT '10.Lucida Console' OBJECT oSay

DCREAD GUI FIT TITLE 'Pixel Test'

RETURN nil

* ---------

PROC appsys ; RETURN

* ---------

DLLFUNCTION GetPixel( @nHDC, @x, @y) USING STDCALL FROM GDI32.DLL

* ---------

STATIC FUNCTION ShowColor( nHDC, aCoords, oSay )

LOCAL nColor

IF Empty(nHDC)
  RETURN nil
ENDIF

nColor := GetPixel(@nHDC,@aCoords[1],@aCoords[2])

oSay:setCaption(Alltrim(Str(nColor)) + '  ' + DC_Array2String(aCoords))

RETURN nil
colors.jpg
colors.jpg (8.13 KiB) Viewed 15492 times
The eXpress train is coming - and it has more cars.

User avatar
Auge_Ohr
Posts: 1407
Joined: Wed Feb 24, 2010 3:44 pm

Re: How to know the attributes of a pixel in the image

#7 Post by Auge_Ohr »

rdonnay wrote:I tried using the windows api function GetPixel() but it keeps returning a -1.
you need hDC which is the Device-Content.

Code: Select all

#INCLUDE "xbp.CH"
#INCLUDE "AppEvent.CH"
#INCLUDE "dll.CH"

DLLFUNCTION GetPixel( nHDC, x, y)   USING STDCALL FROM GDI32.DLL
DLLFUNCTION GetWindowDC( hwnd )     USING STDCALL FROM USER32.DLL
DLLFUNCTION ReleaseDC(hwnd, hdc)    USING STDCALL FROM USER32.DLL

PROCEDURE AppSys
RETURN

PROCEDURE Main
LOCAL nEvent, mp1, mp2, oXbp
LOCAL oDlg
LOCAL aSize := {225,225}  // size of Demo JPG
LOCAL oImage
LOCAL oStatic

   oDlg         := XbpDialog():new( AppDesktop() )
   oDlg:title   := "GetPixel"
   oDlg:tasklist := .T.
   oDlg:clientSize := aSize
   oDlg:create()
   CenterControl(oDlg)

   oImage := XbpBitmap():new():create()
   oImage:loadfile("colors.jpg" )

   oStatic := XbpStatic():new(oDlg:drawingArea ,, {0,0} , aSize )
   oStatic:Type := XBPSTATIC_TYPE_BITMAP
   oStatic:Caption := oImage
   oStatic:Create()
   // aPos is "relative" to Parent
   oStatic:lbDown := {|aPos, uNIL, oSelf|ShowColor(aPos,oSelf)}

   nEvent := 0
   DO WHILE nEvent <> xbeP_Close
      nEvent := AppEvent( @mp1, @mp2, @oXbp )
      oXbp:handleEvent( nEvent, mp1, mp2 )
   ENDDO

RETURN

FUNCTION ShowColor( aPos, oSelf )
LOCAL oPS
LOCAL hWnd
LOCAL hDC := 0
LOCAL nHandle
LOCAL nColor

   oPS  := XbpPresSpace():new():create( oSelf:winDevice() )
   hWnd := oSelf:getHWND()
   hDC  := GetWindowDC( hWnd )

   nColor := GetPixel(hDC,aPos[1],aPos[2])

   ReleaseDC(hWnd, hDC)
   oPS:Destroy()

   IF nColor > 0
      Msgbox(VAR2CHAR(GraGetRGBIntensity(AutomationTranslateColor(nColor,.T.))))
   ENDIF

RETURN nil
GetPixel.PNG
GetPixel.PNG (78.47 KiB) Viewed 15480 times
greetings by OHR
Jimmy

User avatar
rdonnay
Site Admin
Posts: 4729
Joined: Wed Jan 27, 2010 6:58 pm
Location: Boise, Idaho USA
Contact:

Re: How to know the attributes of a pixel in the image

#8 Post by rdonnay »

you need hDC which is the Device-Content.
I KNEW you would have the answer. Thanks.

Here it is in eXpress++ code. Windows coordinate system is from top. Xbase++ is from bottom.
I had to translate the Y to get the correct coords.

Code: Select all

#INCLUDE "dll.CH"
#INCLUDE "dcdialog.CH"

FUNCTION Main()

LOCAL GetList[0], oSay, hDC

@ 0,0 DCSTATIC TYPE XBPSTATIC_TYPE_BITMAP ;
      CAPTION "colors.jpg" ;
      PREEVAL {|o|o:autoSize := .t.} ;
      EVAL {|o|hDC := GetWindowDC(o:getHWnd()), ;
                   o:motion := {|a,b,o|ShowColor( hDC, a, oSay, o )}}

@ 3,0 DCSAY '' SAYSIZE 50 FONT '10.Lucida Console' OBJECT oSay

DCREAD GUI FIT TITLE 'Pixel Test'

RETURN nil

* ---------

PROC appsys ; RETURN

* ---------

DLLFUNCTION GetPixel( nHDC, x, y) USING STDCALL FROM GDI32.DLL
DLLFUNCTION GetWindowDC( hwnd )     USING STDCALL FROM USER32.DLL

* ---------

STATIC FUNCTION ShowColor( hDC, aCoords, oSay, oStatic )

LOCAL nColor

aCoords[2] := oStatic:currentSize()[2] - aCoords[2]

nColor := GetPixel(hDC,aCoords[1],aCoords[2])

oSay:setCaption('Color: ' + DC_Array2String(GraGetRGBIntensity(AutomationTranslateColor(nColor,.T.))) + ;
   ' Coords: ' + DC_Array2String(aCoords))

RETURN nil
Now that we know how to do this, we need to turn it into a function for Eugene.
This can probably be done where the window is invisible.

example:

Code: Select all

oWindow := MakeWindow():new( 'colors.jpg' )):create()

// Fill an array
aColors := Array(222,222)
FOR i := 1 to 222
  FOR j := 1 to 222
    aColors[i,j] := GetRGBColor( oWindow, i, j )
  NEXT
NEXT

oWindow:destroy()
I need to know more about how this will be used.
The eXpress train is coming - and it has more cars.

User avatar
Auge_Ohr
Posts: 1407
Joined: Wed Feb 24, 2010 3:44 pm

Re: How to know the attributes of a pixel in the image

#9 Post by Auge_Ohr »

rdonnay wrote:

Code: Select all

STATIC FUNCTION ShowColor( hDC, aCoords, oSay, oStatic )
   ...
   aCoords[2] := oStatic:currentSize()[2] - aCoords[2]
   ...
oh ... YES Windows does use "Top-Down" Coordinate, THX.

Code: Select all

LOCAL aSize := oSelf:Currentsize()
   ...
   nColor := GetPixel(hDC,aPos[1],aSize[2]-aPos[2])
   ...
greetings by OHR
Jimmy

User avatar
Eugene Lutsenko
Posts: 1649
Joined: Sat Feb 04, 2012 2:23 am
Location: Russia, Southern federal district, city of Krasnodar
Contact:

Re: How to know the attributes of a pixel in the image

#10 Post by Eugene Lutsenko »

rdonnay wrote:I need to know more about how this will be used.
I've been developing a single system (https://translate.google.com/translate? ... t=&act=url). Previously, she was under DOS. Now it is under Windows. Previously, it was one educational app that I now also wanted to implement. In the sample application, I was on the screen different character sets and software determines which pixels are black and some are white. Pixel values ​​I have seen as a sign of characters. After this, the system formed a model in which each pixel has been calculated amount of information in bits that is a character pixel. Suppose now that there is an image of an unknown character, which is known which pixels in its portrayal of black. The system determines how much information is contained in total in all pixels of images that this image of a symbol. It is believed that this is the symbol of the image which most information. In principle can likewise identify any image not only characters. The number of applications of these technologies very much. This cellular automata, an image search on the Internet, and identification of a person by his photos, etc. All this is very interesting and beautiful.

Here are two of my articles about this:

http://ej.kubagro.ru/2009/07/pdf/05.pdf
http://ej.kubagro.ru/2009/02/pdf/10.pdf

The first article shows the possibility of identifying characters on pixels. The second article shows the possibility of identifying symbols on a symbol, consisting of many pixels. This is equal to one-layer and two-layer neural network. All of these articles are written by way of example with the old system under DOS. A new system under Windows I started doing since June 2012.

Post Reply