0

I have more than 20 images that I want to classify based on pixel correlation. I am able to perform all the procedures but the problem is just saving the image into the corresponding class. Suppose that the pixel correlation values are[0.48, 0.20, 0.57, 0.53, 0.06, 0.52, 0.55, 0.57, 0.51, 0.49, ..., 0.25]

And I want to use each index of the above values based on some thresholds to classify the images into Normal, Abnormal and Ambiguous classes. With this snippet below, I achieved the following results however, The saved images are in an unsupported format

for filename in os.listdir(folder):
if filename.endswith('.jpg'):
    img = cv2.imread(os.path.join(folder, filename))
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
r = []
for correlation in range(len(correlation_matrices)):
    symmetry = correlation_matrices[correlation][0,1]
    r.append(symmetry)
#print(r)

threshold1 = 0.45
threshold2 = 0.35

for index in range(len(r)):

    
    if (rounded_value[index] >= threshold1):
        print('Normal Image')
        cv2.imwrite('./uniform_luminance/Normal' + str(index) + '.jpg', img[index])
    
    elif ((rounded_value[index] < threshold1) and rounded_value[index] >= threshold2):
        print('Ambiguous Image')
        cv2.imwrite('./ambiguous_luminance/Ambiguous' + str(index) + '.jpg', img[index])
    
    elif (rounded_value[index] < threshold2):
        print('Abnormal Image')
        cv2.imwrite('./non_uniform_luminance/Abnormal' + str(index) + '.jpg', img[index])

I achieved the following

The saved images are not similar to the input images however classified based on the thresholds

While the input images as follows

1 Answer 1

1

Are you asking how to correct the format of the saved images? Or why the saved images are different than your input ones?

In case you are asking the later:

Assuming your snippet is inside a loop that reads each image separately:

I believe your error is on img[index]. You are saving the image as a column, including all of the rows thus creating an image as shown. Keep in mind that an image is a 2d array and with [] you can specify which values you want to manipulate.

Image[columns, rows]

Since openvcv documentation says that imwrite takes parameters first the filename and second the image, change your code to:

cv2.imwrite('./uniform_luminance/Normal' + str(index) + '.jpg', img)

or

cv2.imwrite('./uniform_luminance/Normal' + str(index) + '.jpg', img[:,:])

img[:, :] the : is used to include all values.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much for your comment, I used index because when I printed the 3rd line in my code as print(img[0]) it returns a NumPy array
Okay. However, specifying which column to show ( img[0] ) will return all rows of that column. Is this what you want to achieve? I suggest trying: cv2.imshow('2D-Image', img) or cv2.imshow('2D-Image', img[:][:]) and compare it to the output of cv2.imshow('1D-Image', img[0]) so you can visually understand the difference. Please refer to: pythonexamples.org/python-opencv-imshow
Thank you so much for a clear clarification

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.