Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use loop in thresholding operation in OpenCV

I would like to apply a thresholding operation to several grayscale images with different threshold values so that the output, which is displayed as a matplotlib plot, will be 15 or so different images with levels of threshold applied to each. The problem I am facing is that it only displays one image after it runs but if I say print(dst.shape) in the loop it will print 15 image shapes.

I have tried putting the output dst in a list so that I could access them by index dst[2] but this returned an error.

maxValue = 255
dst = []
for thresh in range(0, 255, 51):
    for img in imageB, imageG, imageR:
        th, dst = cv2.threshold(img, thresh, maxValue, cv2.THRESH_BINARY)
        #print(dst.shape)
        #print(len(dst))
        plt.imshow(dst)

What I am trying to achieve is 15 different images from the loop. Is this a matplotlib issue? Am I required to create a figure of a specific size then access each variable in the dst list? If so, why when I print(len(dst)) does it only return the length of the rows in the image?

like image 952
David Alford Avatar asked Jan 02 '26 03:01

David Alford


1 Answers

In the code you've shown you are assigning the thresholded image from cv2.threshold() to the name of the list, hence print(len(dst)) returning information about the lengh of an image rather than the length of a list. You have effectively overwritten your list with an image.

Plotting thresholded images in a loop:

import cv2
import numpy as np
import matplotlib.pyplot as plt

# Make a test image.
r = np.random.randint(0,255,10000).reshape(100,100)
g = np.random.randint(0,255,10000).reshape(100,100)
b = np.random.randint(0,255,10000).reshape(100,100)
img = np.dstack([r,g,b]).astype(np.uint8)

# Convert test image to grayscale.
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

fig, axs = plt.subplots(1,3, figsize=(6,2))
for thresh_value, ax in zip(range(75,255,75), axs.ravel()):
    T, thresh = cv2.threshold(img_gray, thresh_value, 255, cv2.THRESH_BINARY)
    ax.imshow(thresh, cmap='gray')
    ax.set_title(str(thresh_value))

plt.tight_layout()
plt.savefig('plot_img.png')

Producing:

enter image description here

like image 118
Dodge Avatar answered Jan 03 '26 15:01

Dodge



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!