Opencv c++. Draw a circle on different pixel of image in a for loop, (the image should be open new at each loop run)

Gaetan

I want to plot circles on a image where each previous circle is deleted on the image before the next circle is drawn.

I have to following configuration:

  1. I have several picture (let says 10)

  2. For each picture I test several pixel for some condition (let say 50 pixels).

  3. For each pixel I'm testing (or working on) I want to draw a circle at that pixel for visualization purpose (for me to visualize that pixel).

  4. To summarize I have 2 for loop, one looping over the 10 images and the other looping over the 50 pixels.

I done the following (see code above). The circles are correctly drawn but when the next circle is drawn, the previous circle is still visible (at the end all circle are drawn on the same image) but what I want to have is (after a circle was drawn) to close the picture (or window) somehow and reopen a new one and plot the next circle on it and so on

for(int imgID=0; imgID < numbImgs; imgID++)
{
cv::Mat colorImg = imgVector[imgID]; 
for(int pixelID=0; pixelID < numPixelsToBeTested; pixelID++)
{
some_pixel = ... //some pixel 
x = some_pixel(0); y = some_pixel(1); 
cv::Mat colorImg2 = colorImg; //redefine the image for each pixel
cv::circle(colorImg2, cv::Point(x,y),5, cv::Scalar(0,0,255),1, cv::LINE_8, 0);
// creating a new window each time
cv::namedWindow("Display", CV_WINDOW_AUTOSIZE );  
cv::imshow("Display", colorImg2);                           
cv::waitKey(0);
cv::destroyWindow("Display");
}
}

What is wrong in my code? Thanks guys

ZdaR

cv::circle() manipulates the input image within the API call, so what you need to do is to create a clone of the original image, draw circles on the cloned image and at each iteration swap the cloned image with original image.

It is also a good idea to break your program into smaller methods, making the code more readable and easy to understand, Following code may give you a starting point.

void visualizePoints(cv::Mat mat) {
    cv::Mat debugMat = mat.clone();

    // Dummy set of points, to be replace with the 50 points youo are using.
    std::vector<cv::Point> points = {cv::Point(30, 30), cv::Point(30, 100), cv::Point(100, 30), cv::Point(100, 100)};
    for (cv::Point p:points) {
        cv::circle(debugMat, p, 5, cv::Scalar(0, 0, 255), 1, cv::LINE_8, 0);

        cv::imshow("Display", debugMat);
        cv::waitKey(800);
        debugMat = mat.clone();
    }
}

int main() {
    std::vector<std::string> imagePaths = {"path/img1.png", "path/img2.png", "path/img3.png"};
    cv::namedWindow("Display", CV_WINDOW_AUTOSIZE );

    for (std::string path:imagePaths) {
        cv::Mat img = cv::imread(path);
        visualizePoints(img);
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

OpenCV - Loop an image as Webcam Input

From Dev

How can I access each pixel in OpenCV, perform an operation and return the new information to the image?

From Dev

Copy image pixel by pixel in OpenCV

From Dev

Drawing a circle on a particular pixel of image

From Dev

Draw Circle in the background of Text View, over each image in Fragment

From Dev

matplotlib draw is slow in loop when it showing an image

From Dev

draw ROI in image using opencv and c++

From Dev

Image::Magick draw a blurred circle

From Dev

How to draw a circle on an image in android

From Dev

OpenCV reading image pixel value

From Dev

How to speed up 'for loop' for searching Pixel value in Image in python?

From Dev

How to count the number of occurrences of pixel intensities in an image without using for loop?

From Dev

Apply function to each pixel of the image

From Dev

Apply function to each pixel of the image

From Dev

loop with delay should run 5 times each time

From Dev

How to draw a colored pixel on a grayscale image?

From Dev

How to draw curves in circle in canvas using for loop?

From Dev

For each pixel in a image, get the color, pixel position, and coordinates relatively to the image

From Dev

For each pixel in a image, get the color, pixel position, and coordinates relatively to the image

From Dev

Enlarging an image without blurring by taking one pixel and turning it into four on a new image with different dimensions

From Dev

Run a loop for each semicolon

From Dev

Android canvas: draw transparent circle on image

From Dev

Draw Circle onTouch on a Canvas Image Bitmap

From Dev

how can i draw an image pixel by pixel to jframe

From Dev

how can i draw an image pixel by pixel to jframe

From Dev

How to repeat a loop that generates different outputs with each run?

From Dev

Convolve an image several times using for loop opencv python

From Dev

How to open different Gallery for each image using fancybox

From Dev

Searching for a pixel in an image using opencv, numpy and python

Related Related

  1. 1

    OpenCV - Loop an image as Webcam Input

  2. 2

    How can I access each pixel in OpenCV, perform an operation and return the new information to the image?

  3. 3

    Copy image pixel by pixel in OpenCV

  4. 4

    Drawing a circle on a particular pixel of image

  5. 5

    Draw Circle in the background of Text View, over each image in Fragment

  6. 6

    matplotlib draw is slow in loop when it showing an image

  7. 7

    draw ROI in image using opencv and c++

  8. 8

    Image::Magick draw a blurred circle

  9. 9

    How to draw a circle on an image in android

  10. 10

    OpenCV reading image pixel value

  11. 11

    How to speed up 'for loop' for searching Pixel value in Image in python?

  12. 12

    How to count the number of occurrences of pixel intensities in an image without using for loop?

  13. 13

    Apply function to each pixel of the image

  14. 14

    Apply function to each pixel of the image

  15. 15

    loop with delay should run 5 times each time

  16. 16

    How to draw a colored pixel on a grayscale image?

  17. 17

    How to draw curves in circle in canvas using for loop?

  18. 18

    For each pixel in a image, get the color, pixel position, and coordinates relatively to the image

  19. 19

    For each pixel in a image, get the color, pixel position, and coordinates relatively to the image

  20. 20

    Enlarging an image without blurring by taking one pixel and turning it into four on a new image with different dimensions

  21. 21

    Run a loop for each semicolon

  22. 22

    Android canvas: draw transparent circle on image

  23. 23

    Draw Circle onTouch on a Canvas Image Bitmap

  24. 24

    how can i draw an image pixel by pixel to jframe

  25. 25

    how can i draw an image pixel by pixel to jframe

  26. 26

    How to repeat a loop that generates different outputs with each run?

  27. 27

    Convolve an image several times using for loop opencv python

  28. 28

    How to open different Gallery for each image using fancybox

  29. 29

    Searching for a pixel in an image using opencv, numpy and python

HotTag

Archive