Skip to content Skip to sidebar Skip to footer

Python Opencv-finding Circle (sun) , Coordinates Of Center The Circle From Picture

I am new here and a little bit newbie in programming. I have one question. I have picture of Sun in bmp file and 16 bit. The picture look as white circle with black backround. I

Solution 1:

The main problem here is finding a good range for your radius. You may have a look at your picture and guess the Radius.

From the Picture you have given I would guess 180 - 220 would be a good range.

Your code would look like:

cv.HoughCircles(processed, storage, cv.CV_HOUGH_GRADIENT, 1, 16.0, 180, 220)

Just try to find good Values for minRadius and maxRadius and this should work fine.

Solution 2:

here is solution of my problem

import numpy as np
import cv2

im = cv2.imread('sun0016.bmp')
height, width, depth = im.shape
print height, width, depth
thresh = 132
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(imgray,(5,5),0)
edges = cv2.Canny(blur,thresh,thresh*2)
contours, hierarchy = cv2.findContours(edges,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cnt = contours[0]
cv2.drawContours(im,contours,-1,(0,255,0),-1)

#centroid_x = M10/M00 and centroid_y = M01/M00
M = cv2.moments(cnt)
x = int(M['m10']/M['m00'])
y = int(M['m01']/M['m00'])
print x,y
print width/2.0,height/2.0print width/2-x,height/2-y


cv2.circle(im,(x,y),1,(0,0,255),2)
cv2.putText(im,"center of Sun contour", (x,y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255))
cv2.circle(im,(width/2,height/2),1,(255,0,0),2)
cv2.putText(im,"center of image", (width/2,height/2), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,0,0))
cv2.imshow('contour',im)
cv2.waitKey(0)

Solution 3:

Thought I would chime in with an alternative solution in case anyone stumbles upon this question in the future.

The following function uses cv2.inRange instead of cv2.Canny, and cv2.minEnclosingCircle instead of cv2.moments. It selects the largest contour found by cv2.findContours by measuring the radius of candidates' minimum enclosing circle. This filtering helps reject false positives from e.g. watermarks or dust, but depending on your requirements you might want to go about this step differently or omit it entirely.

The function returns both the x,y coordinates as well as the radius of the detected disk, a requirement for the project I was working on.

import cv2


deffind_disk(img, threshold=10):
    """Finds the center and radius of a single solar disk present in the supplied image.

    Uses cv2.inRange, cv2.findContours and cv2.minEnclosingCircle to determine the centre and 
    radius of the solar disk present in the supplied image.

    Args:
        img (numpy.ndarray): greyscale image containing a solar disk against a background that is below `threshold`.
        threshold (int): threshold of min pixel value to consider as part of the solar disk

    Returns:
        tuple: center coordinates in x,y form (int) 
        int: radius
    """if img isNone:
        raise TypeError("img argument is None - check that the path of the loaded image is correct.")

    iflen(img.shape) > 2:
        raise TypeError("Expected single channel (grayscale) image.")

    blurred = cv2.GaussianBlur(img, (5, 5), 0)
    mask = cv2.inRange(blurred, threshold, 255)
    img_mod, contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    # Find and use the biggest contour
    r = 0for cnt in contours:
        (c_x, c_y), c_r = cv2.minEnclosingCircle(cnt)
        # cv2.circle(img, (round(c_x), round(c_y)), round(c_r), (255, 255, 255), 2)if c_r > r:
            x = c_x
            y = c_y
            r = c_r

    # print("Number of contours found: {}".format(len(contours)))# cv2.imwrite("mask.jpg", mask)# cv2.imwrite("circled_contours.jpg", img)if x isNone:
        raise RuntimeError("No disks detected in the image.")

    return (round(x), round(y)), round(r)


if __name__ == "__main__":
    image = cv2.imread("path/to/your/image.jpg")
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    center, radius = find_disk(img=gray, threshold=20)

    print("circle x,y: {},{}".format(center[0], center[1]))
    print("circle radius: {}".format(radius))

    # Output the original image with the detected disk superimposed
    cv2.circle(image, center, radius, (0, 0, 255), 1)
    cv2.rectangle(image, (center[0] - 2, center[1] - 2), (center[0] + 2, center[1] + 2), (0, 0, 255), -1)
    cv2.imwrite("disk_superimposed.jpg", image)

I have left in some commented debug statements that may come in handy if you find the need to tinker with this further.

You might want to use a higher threshold if your images include a lot of glare.

Post a Comment for "Python Opencv-finding Circle (sun) , Coordinates Of Center The Circle From Picture"