1

i am trying to detect boundaries of an ID card in an image using the code below. The key is the gamma value i use.. i use a value of 2 or 3 (given that i want the card to stand out against the background). I run into an issue while using photos with backgrounds lighter or as light as the card color itself. Kindly look at the images below .. the first one is the orig with a dark b/g and the 2nd one is with the gamma correction .. same with the next 2. I am at my wits end trying to figure out how i could handle pics with lighter background. Also pasting the code i use to perform gamma correction. Kindly let me know if you folks can point my thick head in the right direction :)

import cv2
import numpy as np
import imutils
import math
import sys

img = cv2.imread( sys.argv[1] )
gray1 = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
invGamma = 3.0
table = np.array([((i / 255.0) ** invGamma) * 255
for i in np.arange(0, 256)]).astype("uint8")

gray = cv2.LUT(gray1, table)
ret,thresh1 = cv2.threshold( gray, 80, 255, cv2.THRESH_BINARY )
cv2.imwrite( 'LUT.jpg', thresh1 )
_, contours, hierarchy = cv2.findContours(thresh1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

Image1

Image1-Gamma-corrected-3

Image2

Image2-Gamma-3

1 Answer 1

9

as I understand your question, you are mainly trying to detect the edges of the card? Or are you trying to separate the card completely (i.e. cut out) from the image?

Your use of gamma is simply changing apparent contrast of the entire image after you convert it to gray. OpenCV has a lot of edge and object detection routines of its own, as I am assuming you are trying to do pre-processing to help?

A CONCEPTS (Not Code) ANSWER:

SEPARATE CHANNELS

Let me point you in a different direction. If the input images are always color, consider using only one of the three RGB color channels. Here's an example:

RED CHANNEL:

RedChannelOnly

GREEN CHANNEL:

BlueChannelOnly

BLUE CHANNEL:

enter image description here

Notice how much more contrast the blue channel has relative to the red channel. Depending on the image contents, you will typically find that one channel has better separation for the desired object.

If you look at the histogram:

Histogram

You can see that the blue channel has the greatest distance between the peaks on the right (the desired object) and the left peak (bright spot on the table). But the red channel has everything bunched up in the middle.

As an idea, you could use peak detection/peak location/distance between peaks to programmatically determine relative contrasts in each color channel.

You can also determine which color channel has its peak the farthest from the same peak in another channel, and then SUBTRACT or use DIFFERENCE or DIVIDE the two channels (example of this in "channel math" below).


The Table Cloth

Now with the table cloth, it has a very high contrast pattern on it — white(gray) that is lighter than the ID card and green that is darker than the card. The card is mostly in between.

Using a curves tool from an image editor to provide a graphical example, you can see that clamping the lower AND upper levels to black, you can isolate the mid-ranged card.

But again, notice the histogram:

Histogram

While nearly everything is clumped in the midrange values, the RED channel does have a small peak near black. Using this as a guide, we turn off the green and blue channels, and then CLAMP values below and values above the range of the card's value.

CURVES TOOL:

Curves

And then the resultant red channel only - note this is inverted to make the resulting contrast more clear:

Inverted Red Only


SUMMARY

So the jist of these isolation concepts is to

  1. Examine each color channel to determine which one has best contrast. This will be looking for a peak that is most different from the other channels AND/OR looking for peaks in a channel with the widest "valley" between them.
  2. Clamp low and high values to isolate the desired object. This will use the peaks found in A, with a threshold, to determine the points to clamp and ramp into the desired image.

The problem with just using a general "gamma" adjustment is that you're going to be dragging the entire image around to change apparent contrast, when what you are really trying to do is eliminate (clamp) portions of the image that are not relevant.

While I realize this isn't exactly what you were asking, I hope it was useful nevertheless. Also, I'd suggest checking out more of the detection functions in OpenCV if you aren't already.


BONUS: FUN WITH CHANNEL MATH

This may or may not have utility for you, but multiplying, dividing, subtracting, difference, exclusion between color channels can sometimes help get rid of unwanted background objects. Take the table cloth and the pattern.

Lets MULTIPLY the GREEN and BLUE channels and get THIS:

Multiply Green and Blue

Now lets DIVIDE by the RED channel

Divide by Red Divide

Now adjust the GAMMA of the RED channel to eliminate the tablecloth pattern (Gamma Adjusted to 1.57):

Gamma Adjusted to 1.57

With this resultant image:

Final

Which of course can have further contrast enhancement now that the tablecloth pattern is gone.

3
  • 1
    woah ! amazing .. great detailing ..i cant thank you enough for the response :) May 1, 2019 at 10:39
  • @VikramMurthy, I see the uploaded image, You should mask your DOB, PAN info etc before uploading to any public website. If possible use a dummy image from internet to understand the concept. If it's possible delete the image or the question to avoid any issues later. Delete it if you uploaded to a public website.
    – nircraft
    Oct 27, 2020 at 20:12
  • 1
    @nircraft If Vikram redacts the images, I'll definitely redact the answer images as well so the question/answer can remain.
    – Myndex
    Oct 27, 2020 at 23:35

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.