Help With opencv

Just starting out? Need help? Post your questions and find answers here.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Help With opencv

Post by collectordave »

I have thousands of images and I would like to use opencv to find an image in these images that matches with a selected image.

Looked at "cv_matchtemplate.pb" but I have no idea how it works or how to implement it. Seems to be what I need.

My thoughts are to create a database of some kind that stores the characteristics of the images in the collection then when an image is selected get this images characteristics and compare them to the images in the database.

Can anyone help?

Kind Regards

CD
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: Help With opencv

Post by JHPJHP »

Hi collectordave,

Take a look at cv_norm.pb, might be better suited for your requirements...

Press the Spacebar:
- compares a folder of images against a single image
- returns the best match first
- continue pressing the Spacebar for the next best match

Also, see this thread and the work by wilbert: Search similitary of a picture in folder of thousand others.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Help With opencv

Post by collectordave »

Hi

Thanks JHPJHP

That looks to be very close to what I am looking for. Will go through the code to see if I can understand it.

Had a look at the link and I am allready using wilberts code which I have found to be excellent just attempting to get it to be xplatform as an application.

I have been told that opencv is better than that which I find hard to believe, but just to be sure I am looking at opencv and getting confused.

Thanks again.

CD
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Help With opencv

Post by collectordave »

Hi

Getting into the idea a little more. I think what I need is a mix of wilberts code and maybe some openCV bits.

My problem is that the images to match to are held on a remote server and there are quite a few of them, I can download each image. The image to be matched will be scanned in by a user and I need a quick match which does not need to be perfect just close. The quality of both can be quite dire, but when I crop the images, both the downloaded and user, and then use wilberts code it works very well.

Looked at "cv_bounding_rectangle.pb" and on several test images it draws an outer bounding rectangle in just the right place, it draws lots of others as well but not needed.

My question is, Is it possible to automatically crop an image to the outer bounding rectangle? No need to save the image just need cropped image to get hash.

Hopefully

CD
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: Help With opencv

Post by JHPJHP »

Hi collectordave,

Creating a filter in OpenCV to crop an image is easy, but a filter for thousands of images requires more information.
- is there a general shape, color, or content, etc. that is evident in every image (not mandatory, but can make things easier)

Are you able to provide an image example, or at least describe the image content and what you would like cropped?
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Help With opencv

Post by collectordave »

Hi JHPJHP

I have managed to get the largest bounding rectangle and grab the image inside by modifying the "cv_bounding_rectangle.pb" example to return the coordinates of the largest rectangle then using PBs GrabImage on the original to grab the image inside. Effectively autocropping the image. With mixed results.

The grabbed image seems to have the DPI reduced. I have mentioned this here http://www.purebasic.fr/english/viewtop ... 13&t=69454

Images with a low resolution give bad results.

The images are of postage stamps. These can be in any shape, size and DPI. There are triangular, circular, octagonal and some weird shapes, physical size also varies from 6mm X 6mm to I believe 150mm X 150mm. Not all have a white boundary and not all have perforations at the sides. The images are also scanned on a variety of backgrounds some blue, red some are even photographs with variable backgrounds. The size of these backgrounds/borders varies greatly dependent upon the skill of the person scanning the stamp, it is this I want to remove completly.

My ideal solution would be a procedure to do the following:-

1. Load Image
2. Reject if DPI less than say 200DPI
3. Crop image to the largest bounding rectangle if this is at least 80% of the area of the whole image.
4. Create and store the aspect ratio and a hash of the resultant image in a database.
5. Move correctly processed images to a second folder

I am using wilberts code to create a hash at the moment and straightforward PB code for the aspect ratio.

The aspect ratio and hash are used later by others to compare their image with images allready "learnt". Their image will undergo the same transformation and a hash produced which is compared to the originals in the database. Without the copying.

This reduces well over 40Gbytes of images to a database of just over 40Mbytes much easier to distribute etc.

Will post a zip of various images later.

Kind regards

CD
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Help With opencv

Post by collectordave »

Hi JHPJHP

Here is a dropbox link to a few images if you need more let me know.https://www.dropbox.com/s/rorlfd88cvj6b ... s.zip?dl=0

The package has the strting image and the cropped image but raises another question the cropped images are smaller and have a reduced dpi but the file sizes are larger?

Hope these help.

regards

CD
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: Help With opencv

Post by JHPJHP »

Hi collectordave,

This is a modified version of the example cv_bounding_rectangle.pb; tested against the three provided images.
- iterates through a directory of JPG images
- applies an offset to the cropped images (currently 4)
- kept most of the perforations, but can easily be removed
- creates a new directory: \Cropped\
- saves the cropped images to the new directory

Post any problem images (various parameters can be adjusted).

Code: Select all

IncludeFile "includes/cv_functions.pbi"

Procedure CropImage(ImageFile.s, Offset = 0)
  If FileSize(ImageFile) > 0
    *image.IplImage = cvLoadImage(ImageFile, #CV_LOAD_IMAGE_ANYDEPTH | #CV_LOAD_IMAGE_ANYCOLOR)

    If *image\nChannels = 1
      *gray.IplImage = cvCloneImage(*image)
    Else
      *gray.IplImage = cvCreateImage(*image\width, *image\height, #IPL_DEPTH_8U, 1)
      cvCvtColor(*image, *gray, #CV_BGR2GRAY, 1)
    EndIf
    cvThreshold(*gray, *gray, 150, 255, #CV_THRESH_BINARY)
    cvSmooth(*gray, *gray, #CV_GAUSSIAN, 3, 0, 0, 0)
    *storage.CvMemStorage = cvCreateMemStorage(0)
    *contours.CvContour
    nContours = cvFindContours(*gray, *storage, @*contours, SizeOf(CvContour), #CV_RETR_EXTERNAL, #CV_CHAIN_APPROX_NONE, 0, 0)

    If nContours
      *contour.IplImage = cvCreateImage(*image\width, *image\height, #IPL_DEPTH_8U, 3)

      For rtnCount = 0 To nContours - 1
        area.d = cvContourArea(*contours, 0, #CV_WHOLE_SEQ_END_INDEX, 0)

        If area > 10000
          *crop.IplImage = cvCreateImage(*contours\rect\width - Offset * 2, *contours\rect\height - Offset * 2, #IPL_DEPTH_8U, 3)
          cvSetImageROI(*image, *contours\rect\x + Offset, *contours\rect\y + Offset, *contours\rect\width - Offset * 2, *contours\rect\height - Offset * 2)
          cvCopy(*image, *crop, #Null)
          cvResetImageROI(*image)
          params.CvSaveData
          params\paramId = #CV_IMWRITE_JPEG_QUALITY
          params\paramValue = 95
          cvSaveImage(GetPathPart(ImageFile) + "Cropped\" + GetFilePart(ImageFile), *crop, @params)
          cvReleaseImage(@*crop) : Break
        EndIf
        *contours = *contours\h_next
      Next
      cvReleaseImage(@*contour)
    EndIf
    cvReleaseMemStorage(@*storage)
    cvReleaseImage(@*gray)
    cvReleaseImage(@*image)
  EndIf
EndProcedure

ImageLocation.s = PathRequester("Select your JPG Folder", GetCurrentDirectory())

If FileSize(ImageLocation + "Cropped\") <> -2 : CreateDirectory(ImageLocation + "Cropped\") : EndIf

If ExamineDirectory(0, ImageLocation, "*.jpg")
  While NextDirectoryEntry(0)
    If DirectoryEntryType(0) = #PB_DirectoryEntry_File : CropImage(ImageLocation + DirectoryEntryName(0), 4) : EndIf
  Wend
  FinishDirectory(0)
EndIf
Last edited by JHPJHP on Wed Oct 25, 2017 5:15 pm, edited 1 time in total.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Help With opencv

Post by collectordave »

Tyanks JHPJHP

I am getting lots of POLINK errors when tryimg to run the standalone version for some of the cv procedures.

When I run the posted code get cvcreateimage wrong number of parameters?

Any ideas?

Regards

cd
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: Help With opencv

Post by JHPJHP »

Hi collectordave,

I used the 32 bit interface; currently the example must be executed from PureBasic (x86), but can easily be adapted to the x64 IDE.
Last edited by JHPJHP on Sat Oct 28, 2017 1:05 am, edited 1 time in total.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Help With opencv

Post by collectordave »

Hi JHpJHP

Just tried the 64 bit version on windows:- Amazing! exactly what I was looking for.

Thank you so much.

Will run it against all 600000 images and single image as well.

How good is that.

Kind Regards

CD
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
Post Reply