Hi!
I am running a code using the cv2 module to count red dots in images. All my files are in .tif format. Some images were dotted by myself, and others by another person. My problem is that my own images run without any problem, but the images that I received from the other person don’t work in the loop. However, if I open their images on the Idle interface command line it works without problem. I already re-saved the images in exactly the same program and when I look into the properties of the images they look exactly the same… Has anyone any idea what could be the problem? I attach below my code and interface commands. (python 3.10.4)
Thank you very much.
LOOP FILE:
import cv2
import numpy
import os
from os import listdir
# assign colors
red = [(0,0,220),(60,60,265)] # lower and upper
dot_colors = [red]
# get the path/directory
folder_dir = "C:/Users/x/OneDrive/Documents/test_photos"
for images in os.listdir(folder_dir):
print(folder_dir)
print(images)
img = cv2.imread(images)
# apply medianBlur to smooth image before threshholding
blur= cv2.medianBlur(img, 5) # smooth image by 7x7 pixels, may need to adjust a bit
for lower, upper in dot_colors:
output = img.copy()
# apply threshhold color to white (255,255, 255) and the rest to black(0,0,0)
mask = cv2.inRange(blur,lower,upper)
circles = cv2.HoughCircles(mask,cv2.HOUGH_GRADIENT,1,20,param1=20,param2=8,
minRadius=0,maxRadius=60)
index = 0
if circles is not None:
# convert the (x, y) coordinates and radius of the circles to integers
circles = numpy.round(circles[0, :]).astype("int")
# loop over the (x, y) coordinates and radius of the circles
for (x, y, r) in circles:
# draw the circle in the output image,
# then draw a rectangle corresponding to the center of the circle
cv2.circle(output, (x, y), r, (255, 0, 255), 2)
cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (255, 0, 255), -1)
index = index + 1
#print str(index) + " : " + str(r) + ", (x,y) = " + str(x) + ', ' + str(y)
print(images)
print('No. of circles detected = {}'.format(index))
output:
C:/Users/x/OneDrive/Documents/test_photos
AB2-3_A3.tif
AB2-3_A3.tif
No. of circles detected = 170
C:/Users/x/OneDrive/Documents/test_photos
AB2-3_B1.tif
AB2-3_B1.tif
No. of circles detected = 170
C:/Users/x/OneDrive/Documents/test_photos
FM2_C1.tif
Traceback (most recent call last):
File "C:\Users\x\OneDrive\Documents\script_loop.py", line 21, in <module>
blur= cv2.medianBlur(img, 5) # smooth image by 7x7 pixels, may need to adjust a bit
cv2.error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\median_blur.dispatch.cpp:283: error: (-215:Assertion failed) !_src0.empty() in function 'cv::medianBlur'
When I run the specific file in Idle shell it works fine:
image="C:/Users/x/OneDrive/Documents/test_photos/FM2_C1.tif"
img = cv2.imread(image)
print(img)
[[[ 88 136 68]
[134 114 68]
[181 94 47]
...
[190 213 103]
[236 208 51]
[236 204 0]]
[[ 85 158 68]
[107 86 68]
[130 85 47]
...
[201 197 103]
[245 191 51]
[245 174 0]]
[[ 83 91 39]
[ 81 58 39]
[ 79 57 27]
...
[213 181 90]
[255 162 45]
[255 144 0]]
...
[[ 78 116 52]
[103 90 52]
[128 68 49]
...
[143 98 55]
[156 108 58]
[156 87 62]]
[[128 93 44]
[139 67 44]
[150 67 46]
...
[159 87 58]
[175 96 56]
[175 99 54]]
[[128 68 36]
[139 68 36]
[150 67 43]
...
[159 124 62]
[175 93 54]
[175 96 46]]]