Page 1 of 1

Simple Images Compare

Posted: Sun Jun 13, 2010 12:19 pm
by Phantomas
This is simple procedure to Compare two .png Images one size (width & height) pixel by pixel.
Load your images and run:

Code: Select all

compare(#image_one, #image_two, 95)
95 — maximum consistency of images in %, standart — 100 (100% / 1:1 images).
Procedure example:

Code: Select all

UsePNGImageDecoder()

Enumeration
  #image_one
  #image_two
EndEnumeration

Procedure compare(image_one, image_two, maxpercent = 100)
  cur_image = image_one
  Repeat
    If StartDrawing(ImageOutput(cur_image))
      wid = ImageWidth(cur_image)
      hei = ImageHeight(cur_image)
      If cur_image = image_two
        Dim pixels_image_two(wid - 1, hei - 1)
      ElseIf cur_image = image_one
        Dim pixels_image_one(wid - 1, hei - 1)
      EndIf
      For hei_temp = 0 To hei - 1 Step 1
        For wid_temp = 0 To wid - 1 Step 1
          color = Point(wid_temp, hei_temp)
          If cur_image = image_two
            pixels_image_two(wid_temp, hei_temp) = color
          ElseIf cur_image = image_one
            pixels_image_one(wid_temp, hei_temp) = color
          EndIf
        Next
      Next
    StopDrawing()
    EndIf
    FreeImage(cur_image)
    If cur_image = image_two
      Break
    ElseIf cur_image = image_one
      cur_image = image_two
    EndIf
  ForEver
  
  If ArraySize(pixels_image_one()) = ArraySize(pixels_image_two())
    For hei_temp = 0 To hei - 1 Step 1
      For wid_temp = 0 To wid - 1 Step 1
        If pixels_image_one(wid_temp, hei_temp) = pixels_image_two(wid_temp, hei_temp)
          counter + 1
        EndIf
      Next
    Next
    percent.f = hei * wid / 100
    Debug counter / percent.f
    If counter / percent.f >= maxpercent
      ProcedureReturn 1
    EndIf
  EndIf
EndProcedure

If LoadImage(#image_one, "image_one.png") And LoadImage(#image_two, "image_two.png")
  Debug compare(#image_one, #image_two, 95)
EndIf
And GUI example with two images (similar 87%):
Image
Download here:
http://rghost.ru/download/1876502/ac544 ... images.zip

Sorry for my bad English.

Re: Simple Images Compare

Posted: Sun Jun 13, 2010 1:03 pm
by jamirokwai
Phantomas wrote:This is simple procedure to Compare two .png Images one size (width & height) pixel by pixel.
Hi Phantomas,

interesting! You should paint the differences using Plot onto a third image to show the result visually... :-)

Re: Simple Images Compare

Posted: Sun Jun 13, 2010 3:36 pm
by Phantomas
jamirokwai wrote:
Phantomas wrote:This is simple procedure to Compare two .png Images one size (width & height) pixel by pixel.
Hi Phantomas,

interesting! You should paint the differences using Plot onto a third image to show the result visually... :-)
Hi! It's so simply. Just need to edit "counter" (42-44 lines), like as (need to download GUI example arhive for replace main code):

Code: Select all

UsePNGImageDecoder() 

Enumeration
  #window
  #window_result
  #track_gadget
  #text_gadget
  #image_gadget_left
  #image_gadget_right
  #image_gadget_result
  #button_compare
  #image_one
  #image_two
  #image_result
EndEnumeration

#title = "Compare Images"

Procedure compare(image_one, image_two, maxpercent = 100)
  cur_image = image_one
  Repeat
    If StartDrawing(ImageOutput(cur_image))
      wid = ImageWidth(cur_image)
      hei = ImageHeight(cur_image)
      If cur_image = image_two
        Dim pixels_image_two(wid - 1, hei - 1)
      ElseIf cur_image = image_one
        Dim pixels_image_one(wid - 1, hei - 1)
      EndIf
      For hei_temp = 0 To hei - 1 Step 1
        For wid_temp = 0 To wid - 1 Step 1
          color = Point(wid_temp, hei_temp)
          If cur_image = image_two
            pixels_image_two(wid_temp, hei_temp) = color
          ElseIf cur_image = image_one
            pixels_image_one(wid_temp, hei_temp) = color
          EndIf
        Next
      Next
    StopDrawing()
    EndIf
    ;FreeImage(cur_image)
    If cur_image = image_two
      Break
    ElseIf cur_image = image_one
      cur_image = image_two
    EndIf
  ForEver
  
  If ArraySize(pixels_image_one()) = ArraySize(pixels_image_two())
    If CreateImage(#image_result, 120, 120) And StartDrawing(ImageOutput(#image_result))
      DrawImage(ImageID(#image_two), 0, 0, 120, 120)
      For hei_temp = 0 To hei - 1 Step 1
        For wid_temp = 0 To wid - 1 Step 1
          If pixels_image_one(wid_temp, hei_temp) = pixels_image_two(wid_temp, hei_temp)
            counter + 1
          Else
            Plot(wid_temp, hei_temp, RGB(255, 100, 0))
          EndIf
        Next
      Next
    StopDrawing()
    EndIf
    percent.f = hei * wid / 100
    Debug counter / percent.f
    If counter / percent.f >= maxpercent
      ProcedureReturn 1
    EndIf
  EndIf
EndProcedure

Procedure result()
  If OpenWindow(#window_result, #PB_Any, #PB_Any, 140, 140, #title, #PB_Window_ScreenCentered | #PB_Window_Invisible | #PB_Window_SystemMenu)
    ImageGadget(#image_gadget_result, 10, 10, 120, 120, ImageID(#image_result))
    HideWindow(#window_result, 0)
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          End
      EndSelect
    ForEver
  EndIf
EndProcedure

If OpenWindow(#window, #PB_Any, #PB_Any, 390, 195, #title, #PB_Window_ScreenCentered | #PB_Window_Invisible | #PB_Window_SystemMenu)
  
  LoadImage(#image_one, "image_one.png")
  LoadImage(#image_two, "image_two.png")
  TrackBarGadget(#track_gadget, 15, 15, 360, 30, 0, 100)
  TextGadget(#text_gadget, 140, 60, 115, 20, "0%", #PB_Text_Center)
  ImageGadget(#image_gadget_left, 15, 60, 120, 120, ImageID(#image_one), #PB_Image_Border)
  ImageGadget(#image_gadget_right, 255, 60, 120, 120, ImageID(#image_two), #PB_Image_Border)
  ButtonGadget(#button_compare, 150, 105, 90, 30, "Compare")
  
  HideWindow(#window, 0)
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Break
      Case #PB_Event_Gadget
        Select EventGadget()
          Case #track_gadget
            SetGadgetText(#text_gadget, Str(GetGadgetState(#track_gadget)) + "%")
          Case #button_compare
            compare = compare(#image_one, #image_two, GetGadgetState(#track_gadget))
            If compare = 1
              MessageRequester(#title, "Compared >= " + Str(GetGadgetState(#track_gadget)) + "%")
            ElseIf compare = 0
              MessageRequester(#title, "Not compared < " + Str(GetGadgetState(#track_gadget)) + "%")
            EndIf
            result()
        EndSelect
    EndSelect
  ForEver
EndIf
Image
(Bad GUI design, i hasten, this simply example)

Re: Simple Images Compare

Posted: Sun Jun 13, 2010 10:46 pm
by jamirokwai
Phantomas wrote:
jamirokwai wrote:
Phantomas wrote:This is simple procedure to Compare two .png Images one size (width & height) pixel by pixel.
Hi Phantomas,

interesting! You should paint the differences using Plot onto a third image to show the result visually... :-)
Hi! It's so simply. Just need to edit "counter" (42-44 lines), like as (need to download GUI example arhive for replace main code):

Code: Select all

UsePNGImageDecoder() 

Enumeration
  #window
  #window_result
  #track_gadget
  #text_gadget
  #image_gadget_left
  #image_gadget_right
  #image_gadget_result
  #button_compare
  #image_one
  #image_two
  #image_result
EndEnumeration

#title = "Compare Images"

Procedure compare(image_one, image_two, maxpercent = 100)
  cur_image = image_one
  Repeat
    If StartDrawing(ImageOutput(cur_image))
      wid = ImageWidth(cur_image)
      hei = ImageHeight(cur_image)
      If cur_image = image_two
        Dim pixels_image_two(wid - 1, hei - 1)
      ElseIf cur_image = image_one
        Dim pixels_image_one(wid - 1, hei - 1)
      EndIf
      For hei_temp = 0 To hei - 1 Step 1
        For wid_temp = 0 To wid - 1 Step 1
          color = Point(wid_temp, hei_temp)
          If cur_image = image_two
            pixels_image_two(wid_temp, hei_temp) = color
          ElseIf cur_image = image_one
            pixels_image_one(wid_temp, hei_temp) = color
          EndIf
        Next
      Next
    StopDrawing()
    EndIf
    ;FreeImage(cur_image)
    If cur_image = image_two
      Break
    ElseIf cur_image = image_one
      cur_image = image_two
    EndIf
  ForEver
  
  If ArraySize(pixels_image_one()) = ArraySize(pixels_image_two())
    If CreateImage(#image_result, 120, 120) And StartDrawing(ImageOutput(#image_result))
      DrawImage(ImageID(#image_two), 0, 0, 120, 120)
      For hei_temp = 0 To hei - 1 Step 1
        For wid_temp = 0 To wid - 1 Step 1
          If pixels_image_one(wid_temp, hei_temp) = pixels_image_two(wid_temp, hei_temp)
            counter + 1
          Else
            Plot(wid_temp, hei_temp, RGB(255, 100, 0))
          EndIf
        Next
      Next
    StopDrawing()
    EndIf
    percent.f = hei * wid / 100
    Debug counter / percent.f
    If counter / percent.f >= maxpercent
      ProcedureReturn 1
    EndIf
  EndIf
EndProcedure

Procedure result()
  If OpenWindow(#window_result, #PB_Any, #PB_Any, 140, 140, #title, #PB_Window_ScreenCentered | #PB_Window_Invisible | #PB_Window_SystemMenu)
    ImageGadget(#image_gadget_result, 10, 10, 120, 120, ImageID(#image_result))
    HideWindow(#window_result, 0)
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          End
      EndSelect
    ForEver
  EndIf
EndProcedure

If OpenWindow(#window, #PB_Any, #PB_Any, 390, 195, #title, #PB_Window_ScreenCentered | #PB_Window_Invisible | #PB_Window_SystemMenu)
  
  LoadImage(#image_one, "image_one.png")
  LoadImage(#image_two, "image_two.png")
  TrackBarGadget(#track_gadget, 15, 15, 360, 30, 0, 100)
  TextGadget(#text_gadget, 140, 60, 115, 20, "0%", #PB_Text_Center)
  ImageGadget(#image_gadget_left, 15, 60, 120, 120, ImageID(#image_one), #PB_Image_Border)
  ImageGadget(#image_gadget_right, 255, 60, 120, 120, ImageID(#image_two), #PB_Image_Border)
  ButtonGadget(#button_compare, 150, 105, 90, 30, "Compare")
  
  HideWindow(#window, 0)
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Break
      Case #PB_Event_Gadget
        Select EventGadget()
          Case #track_gadget
            SetGadgetText(#text_gadget, Str(GetGadgetState(#track_gadget)) + "%")
          Case #button_compare
            compare = compare(#image_one, #image_two, GetGadgetState(#track_gadget))
            If compare = 1
              MessageRequester(#title, "Compared >= " + Str(GetGadgetState(#track_gadget)) + "%")
            ElseIf compare = 0
              MessageRequester(#title, "Not compared < " + Str(GetGadgetState(#track_gadget)) + "%")
            EndIf
            result()
        EndSelect
    EndSelect
  ForEver
EndIf
Image
(Bad GUI design, i hasten, this simply example)
Great... This is one reason why I really enjoy coding with PureBasic!!!
A few lines of code give you so many possibilities...