Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Drawing several circles in an additive way

Hello ! I'm using cv2 in python to draw circles (representing stars) into a CV_8U image. I'm drawing on average 5k small circles, radius goes from 1px to 6px with antialiasing.

Im taking ~80ms for drawing ~5k or few circles and (e.g.) ~3200ms for ~80k circles. Because each circle has a different size and color, I'm actually calling cv2.circle each time I want to draw a circle which is for sure slowing the program down but no big deal (is there any other alternative to this anyway for different radius and colors?).

The issue is where some circles, for example, two red circles overlap:

circle A with radius=5px and color rgb=(200,10,30) at xy=(200, 300)
circle B with radius=2px and color rgb=( 20, 2, 1) at xy=(200, 300)

If circle A is drawn before and circle B later, because circle B's brightness is so low I will be left with a circumference in red and a very dark interior instead of the ideal of making the second circle values add to the already existing values at that position (like adding two light sources).

My current solution is to draw the circles to a buffer the size of the biggest possible circle and then add that buffer to the specific position in the final image. But I'm wondering if is there a parameter in OpenCV to define how the drawings to the image are made? (to make it additive) and prevent the need for that buffer.

Thanks.

Drawing several circles in an additive way

Hello ! I'm using cv2 in python to draw circles (representing stars) into a CV_8U image. I'm drawing on average 5k small circles, radius goes from 1px to 6px with antialiasing.

Im taking ~80ms for drawing ~5k or few circles and (e.g.) ~3200ms for ~80k circles. Because each circle has a different size and color, I'm actually calling cv2.circle each time I want to draw a circle which is for sure slowing the program down but no big deal (is there any other alternative to this anyway for different radius and colors?).

The issue is where some circles, for example, two red circles overlap:

circle A with radius=5px and color rgb=(200,10,30) at xy=(200, 300)
circle B with radius=2px and color rgb=( 20, 2, 1) at xy=(200, 300)

If circle A is drawn before and circle B later, because circle B's brightness is so low I will be left with a circumference in red and a very dark interior instead of the ideal of making the second circle values add to the already existing values at that position (like adding two light sources).

My current solution which makes my program run an order of magnitude slower compared to the timing I wrote before is to draw the circles to a buffer the size of the biggest possible circle and then add that buffer to the specific position in the final image. But I'm wondering if is there a parameter in OpenCV to define how the drawings to the image are made? (to make it additive) and prevent the need for that buffer.

Thanks.

Drawing several circles in an additive way

Hello ! I'm using cv2 in python to draw circles (representing stars) into a CV_8U image. I'm drawing on average 5k small circles, radius goes from 1px to 6px with antialiasing.

Im taking ~80ms for drawing ~5k or few circles and (e.g.) ~3200ms for ~80k circles. Because each circle has a different size and color, I'm actually calling cv2.circle each time I want to draw a circle which is for sure slowing the program down but no big deal (is there any other alternative to this anyway for different radius and colors?).

The issue is where some circles, for example, two red circles overlap:

circle A with radius=5px and color rgb=(200,10,30) at xy=(200, 300)
circle B with radius=2px and color rgb=( 20, 2, 1) at xy=(200, 300)

If circle A is drawn before and circle B later, because circle B's brightness is so low I will be left with a circumference in red and a very dark interior instead of the ideal of making the second circle values add to the already existing values at that position (like adding two light sources).

My current solution which makes my program run an order of magnitude slower compared to the timing I wrote before is to draw the circles to a buffer the size of the biggest possible circle and then add that buffer to the specific position in the final image. But I'm wondering if is there a parameter in OpenCV to define how the drawings to the image are made? (to make it additive) and prevent the need for that buffer.

Thanks.

EDIT : ( added by @sturkmen to be more clear )

import numpy as np
import cv2 as cv

img = np.zeros((12, 12, 3), np.uint8)
cv.circle( img, (5, 5),  5, (30,10,200), 1 , cv.LINE_AA )
cv.circle( img, (5, 5),  2, ( 1, 2, 20), 1 , cv.LINE_AA )
img0 = cv.resize(img,(240,240), interpolation=cv.INTER_AREA)
cv.circle( img, (5, 5),  2, ( 31, 12, 220), 1 , cv.LINE_AA )
img1 = cv.resize(img,(240,240), interpolation=cv.INTER_AREA)
cv.imshow('img0', img0)
cv.imshow('img1', img1)
cv.waitKey()

cv.destroyAllWindows()

current code result:

image description

desired result:

image description

Drawing several circles in an additive way

Hello ! I'm using cv2 in python to draw circles (representing stars) into a CV_8U image. I'm drawing on average 5k small circles, radius goes from 1px to 6px with antialiasing.

Im taking ~80ms for drawing ~5k or few circles and (e.g.) ~3200ms for ~80k circles. Because each circle has a different size and color, I'm actually calling cv2.circle each time I want to draw a circle which is for sure slowing the program down but no big deal (is there any other alternative to this anyway for different radius and colors?).

The issue is where some circles, for example, two red circles overlap:

circle A with radius=5px and color rgb=(200,10,30) at xy=(200, 300)
circle B with radius=2px and color rgb=( 20, 2, 1) at xy=(200, 300)

If circle A is drawn before and circle B later, because circle B's brightness is so low I will be left with a circumference in red and a very dark interior instead of the ideal of making the second circle values add to the already existing values at that position (like adding two light sources).

My current solution which makes my program run an order of magnitude slower compared to the timing I wrote before is to draw the circles to a buffer the size of the biggest possible circle and then add that buffer to the specific position in the final image. But I'm wondering if is there a parameter in OpenCV to define how the drawings to the image are made? (to make it additive) and prevent the need for that buffer.

Thanks.

EDIT : ( added by @sturkmen to be more clear )) EDIT2: Update on the edit

import numpy as np
import cv2 as cv

img img0 = np.zeros((12, 12, 3), np.uint8)
cv.circle( img, cv.circle(img0, (5, 5),  5, (30,10,200), 1 , cv.LINE_AA )
cv.circle( img, (30, 10, 200), -1, cv.LINE_AA)  # Big bright
cv.circle(img0, (5, 5),  2, ( 1, (1, 2, 30), -1, cv.LINE_AA)  # Small dim
img0 = cv.resize(img0, (240, 240), interpolation=cv.INTER_AREA)
cv.imshow('current', img0)

img1 = np.zeros((12, 12, 3), np.uint8)
img2 = np.zeros((12, 12, 3), np.uint8)
cv.circle(img1, (5, 5), 5, (30, 10, 200), -1, cv.LINE_AA)  # Big bright
cv.circle(img2, (5, 5), 2, (1, 2, 20), 1 , cv.LINE_AA )
img0 -1, cv.LINE_AA)  # Small dim
img2 = cv.resize(img,(240,240), interpolation=cv.INTER_AREA)
cv.circle( img, (5, 5),  2, ( 31, 12, 220), 1 , cv.LINE_AA )
img1 = cv.resize(img,(240,240), interpolation=cv.INTER_AREA)
cv.imshow('img0', img0)
cv.imshow('img1', img1)
cv.resize(img1 + img2, (240, 240), interpolation=cv.INTER_AREA)  # Img needs to be clipped [0,255]
cv.imshow('desired', img2)
cv.waitKey()

cv.destroyAllWindows()

current code result:

image descriptionimage description

desired result:

image descriptionimage description

Drawing several circles in an additive way

Hello ! I'm using cv2 in python to draw circles (representing stars) into a CV_8U image. I'm drawing on average 5k small circles, radius goes from 1px to 6px with antialiasing.

Im taking ~80ms for drawing ~5k or few circles and (e.g.) ~3200ms for ~80k circles. Because each circle has a different size and color, I'm actually calling cv2.circle each time I want to draw a circle which is for sure slowing the program down but no big deal (is there any other alternative to this anyway for different radius and colors?).

The issue is where some circles, for example, two red circles overlap:

circle A with radius=5px and color rgb=(200,10,30) at xy=(200, 300)
circle B with radius=2px and color rgb=( 20, 2, 1) at xy=(200, 300)

If circle A is drawn before and circle B later, because circle B's brightness is so low I will be left with a circumference in red and a very dark interior instead of the ideal of making the second circle values add to the already existing values at that position (like adding two light sources).

My current solution which makes my program run an order of magnitude slower compared to the timing I wrote before is to draw the circles to a buffer the size of the biggest possible circle and then add that buffer to the specific position in the final image. But I'm wondering if is there a parameter in OpenCV to define how the drawings to the image are made? (to make it additive) and prevent the need for that buffer.

Thanks.

EDIT : ( added by @sturkmen to be more clear ) EDIT2: Update on the edit)

import numpy as np
import cv2 as cv

img0 img = np.zeros((12, 12, 3), np.uint8)
cv.circle(img0, cv.circle( img, (5, 5),  5, (30, 10, 200), -1, cv.LINE_AA)  # Big bright
cv.circle(img0, (30,10,200), 1 , cv.LINE_AA )
cv.circle( img, (5, 5),  2, (1, ( 1, 2, 30), -1, cv.LINE_AA)  # Small dim
20), 1 , cv.LINE_AA )
img0 = cv.resize(img0, (240, 240), cv.resize(img,(240,240), interpolation=cv.INTER_AREA)
cv.imshow('current', img0)

cv.circle( img, (5, 5),  2, ( 31, 12, 220), 1 , cv.LINE_AA )
img1 = np.zeros((12, 12, 3), np.uint8)
img2 = np.zeros((12, 12, 3), np.uint8)
cv.circle(img1, (5, 5), 5, (30, 10, 200), -1, cv.LINE_AA)  # Big bright
cv.circle(img2, (5, 5), 2, (1, 2, 20), -1, cv.LINE_AA)  # Small dim
img2 = cv.resize(img1 + img2, (240, 240), interpolation=cv.INTER_AREA)  # Img needs to be clipped [0,255]
cv.imshow('desired', img2)
cv.resize(img,(240,240), interpolation=cv.INTER_AREA)
cv.imshow('img0', img0)
cv.imshow('img1', img1)
cv.waitKey()

cv.destroyAllWindows()

current code result:

image description

desired result:

image description

Drawing several circles in an additive way

Hello ! I'm using cv2 in python to draw circles (representing stars) into a CV_8U image. I'm drawing on average 5k small circles, radius goes from 1px to 6px with antialiasing.

Im taking ~80ms for drawing ~5k or few circles and (e.g.) ~3200ms for ~80k circles. Because each circle has a different size and color, I'm actually calling cv2.circle each time I want to draw a circle which is for sure slowing the program down but no big deal (is there any other alternative to this anyway for different radius and colors?).

The issue is where some circles, for example, two red circles overlap:

circle A with radius=5px and color rgb=(200,10,30) at xy=(200, 300)
circle B with radius=2px and color rgb=( 20, 2, 1) at xy=(200, 300)

If circle A is drawn before and circle B later, because circle B's brightness is so low I will be left with a circumference in red and a very dark interior instead of the ideal of making the second circle values add to the already existing values at that position (like adding two light sources).

My current solution which makes my program run an order of magnitude slower compared to the timing I wrote before is to draw the circles to a buffer the size of the biggest possible circle and then add that buffer to the specific position in the final image. But I'm wondering if is there a parameter in OpenCV to define how the drawings to the image are made? (to make it additive) and prevent the need for that buffer.

Thanks.

EDIT : ( added by @sturkmen to be more clear )) EDIT2: Update on the edit

import numpy as np
import cv2 as cv

img img0 = np.zeros((12, 12, 3), np.uint8)
cv.circle( img, cv.circle(img0, (5, 5),  5, (30,10,200), 1 , cv.LINE_AA )
cv.circle( img, (30, 10, 200), -1, cv.LINE_AA)  # Big bright
cv.circle(img0, (5, 5),  2, ( 1, (1, 2, 30), -1, cv.LINE_AA)  # Small dim
img0 = cv.resize(img0, (240, 240), interpolation=cv.INTER_AREA)
cv.imshow('current', img0)

img1 = np.zeros((12, 12, 3), np.uint8)
img2 = np.zeros((12, 12, 3), np.uint8)
cv.circle(img1, (5, 5), 5, (30, 10, 200), -1, cv.LINE_AA)  # Big bright
cv.circle(img2, (5, 5), 2, (1, 2, 20), 1 , cv.LINE_AA )
img0 -1, cv.LINE_AA)  # Small dim
img2 = cv.resize(img,(240,240), interpolation=cv.INTER_AREA)
cv.circle( img, (5, 5),  2, ( 31, 12, 220), 1 , cv.LINE_AA )
img1 = cv.resize(img,(240,240), interpolation=cv.INTER_AREA)
cv.imshow('img0', img0)
cv.imshow('img1', img1)
cv.resize(img1 + img2, (240, 240), interpolation=cv.INTER_AREA)  # Img needs to be clipped [0,255]
cv.imshow('desired', img2)
cv.waitKey()

cv.destroyAllWindows()

current code result:

image description

desired result:

image description