基于Python+OpenCV的视神经胶质瘤识别:圆内白色像素计算求助
Solution: Count White Pixels Inside the Drawn Circle
Got it, let's adjust your code to accurately count only the white pixels inside that blue circle you've drawn. The key issues in your current code are:
- You're checking pixels on the
imagevariable (which has the blue circle drawn on it, altering some pixel values) instead of the original image. - You're iterating over the entire image instead of limiting to the circle's area.
Here are two efficient ways to solve this:
Method 1: Use a Mask (Most Efficient for Large Images)
This leverages OpenCV's optimized functions to avoid slow Python loops:
import cv2 import numpy as np import imutils # Assume your original code up to drawing the circle is here: # gray = cv2.cvtColor(orig, cv2.COLOR_BGR2GRAY) # I'm assuming you had this step to get gray gray = cv2.GaussianBlur(gray, (371, 371), 0) (minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(gray) image = orig.copy() cv2.circle(image, maxLoc, 371, (255, 0, 0), 2) # Define circle parameters center = maxLoc radius = 371 sought = [254, 254, 254] # Step 1: Create a mask that only keeps the area inside the circle mask = np.zeros(orig.shape[:2], dtype=np.uint8) cv2.circle(mask, center, radius, 255, -1) # -1 fills the circle with white # Step 2: Isolate white pixels in the original image white_pixels = cv2.inRange(orig, np.array(sought), np.array(sought)) # Step 3: Keep only white pixels that are inside the circle circle_white_pixels = cv2.bitwise_and(white_pixels, white_pixels, mask=mask) # Step 4: Count the non-zero (white) pixels amount = cv2.countNonZero(circle_white_pixels) print(f"White pixels inside circle: {amount}") # Rest of your display code image = imutils.resize(image, width=400) cv2.imshow("Optic Image", image) cv2.waitKey(0) cv2.destroyAllWindows()
Method 2: Iterate with Circle Boundary Check (For Smaller Images)
If you prefer to keep the loop approach, fix it to check if pixels are inside the circle and use the original image:
import cv2 import imutils # Your original setup code # gray = cv2.cvtColor(orig, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (371, 371), 0) (minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(gray) image = orig.copy() cv2.circle(image, maxLoc, 371, (255, 0, 0), 2) center_x, center_y = maxLoc radius = 371 radius_sq = radius ** 2 # Precompute square to speed up calculations sought = (254, 254, 254) amount = 0 # Iterate over each pixel, but only check those inside the circle for y in range(orig.shape[0]): for x in range(orig.shape[1]): # Calculate distance squared to center (avoids sqrt for speed) dist_sq = (x - center_x) ** 2 + (y - center_y) ** 2 if dist_sq <= radius_sq: b, g, r = orig[y, x] # Use original image, not the one with the blue circle! if (b, g, r) == sought: amount += 1 print(f"White pixels inside circle: {amount}") # Display code image = imutils.resize(image, width=400) cv2.imshow("Optic Image", image) cv2.waitKey(0) cv2.destroyAllWindows()
Key Notes:
- Always use the original image (
orig) for pixel checks: Theimagevariable has the blue circle drawn on it, so its pixel values are modified along the circle's edge. - Mask method is faster: For large medical images, OpenCV's C-optimized functions will outperform Python loops by a huge margin.
- Distance squared trick: Comparing
(x - cx)^2 + (y - cy)^2 <= r^2avoids computing square roots, which saves computation time.
内容的提问来源于stack exchange,提问作者AmyllaVimiar
相关产品推荐
相关产品推荐

