1

I have performed elementwise operations on my image. (Essentially I implemented an algorithm to remove smudge marks. note that avggrad, I0avggrad, avg and I0avg are all of mat type containing floats as elements)

a = avggrad/I0avggrad
b = avg - I0avg * a

originalImg = cv2.imread("testImage.jpg",0) #load as a grayscale
cleanImg = (originalImg - b)/a
cv2.imwrite("Step1.jpg",cleanImg) # the image opens up in gimp as a cleaned up image

After using this I want to perform histogram equalization

img = cv2.equalizeHist(cleanImg)

But I cannot do that as I get the following error

"Assertion failed (_src.type() == CV_8UC1)"

I realize it is because of certain inconsistencies in the image format. In the short term I have a small work around. I save the image and load the saved image, and then it works. But I want to know a better method. To be verbose, I'll write down this code as well

As additional information I am giving the output of the matrices when I print them

output of

print cleanImg

is

[[   0 -123 -121 ..., -126 -117    0]
 [ 119  126 -127 ..., -127 -124 -121]
 [ 122 -128 -125 ..., -123 -126 -117]
 ..., 
 [-126  127  126 ..., -124 -125 -125]
 [-128 -127  127 ..., -123 -123 -121]
 [   0 -126  127 ..., -120 -123    0]]

output of the following code

reloadedImage = cv2.imread("Step1.jpg",0)
print reloadedImage

is:

[[  0 135 136 ..., 129 138   0]
 [121 126 128 ..., 130 136 136]
 [122 124 133 ..., 135 129 140]
 ..., 
 [128 124 129 ..., 135 128 131]
 [131 131 126 ..., 131 136 137]
 [  0 132 129 ..., 135 134   0]]

Obviously there is a conversion going on. Can anyone suggest a better way to go about this? Maybe I should try converting the matrices "avggrad, I0avggrad, avg and I0avg" to integer and then continue?

Also Please suggest how to solve a similar problem in C++ as I have to implement this in C++ later and I'm sure it might help someone else who comes across a similar problem. (as far as i am aware of, a function called convertTo might help, am I right?)

Parth Sane
  • 13
  • 5

1 Answers1

0

As was mentioned in the comments, you need to convert your matrices to uchars before using equalizeHist()

C++ version:

A.convertTo(A, CV_8UC1);

Python version:

A = A.astype(np.uint8)

I also have noticed that some of your matrices have negative entries and are in range [-128, 127]. You may want to shift them to [0, 255] range to avoid losing information.

alexisrozhkov
  • 1,623
  • 12
  • 18