-1

I'm trying to get the pixel's values and their x and y coordinate inside the bounding box of objects.

here's my part of the code :

 vector<Detector::Object> detected_objects;

 for (int i = 0; i < detected_objects.size(); ++i) {
    int xmin = detected_objects[i].rect.x;
    int ymin = detected_objects[i].rect.y;
    int width = detected_objects[i].rect.width;
    int height = detected_objects[i].rect.height;
    Rect rect(xmin, ymin, width, height); //The upper left coordinates (x, y) and the length (x) and width (y) of the rectangle
    cv::rectangle(osrc, rect, Scalar(200, 200, 10), 1, LINE_8, 0); // set rectangle color
   // std::cout << "\n coord: \n" << rect;
    //std::cout << "# of contour points: " << rect.size() << std::endl;
    int xmax = xmin + width;
    int ymax = ymin + height;
    for (size_t x = xmin; x < xmax; x++)
    {
        for (size_t y = ymin; y < ymax; y++)
        {

        }
    }
}

Any help is appreciated!

Jeffrey
  • 11,063
  • 1
  • 21
  • 42
  • Make an example by creating 5 pixels. 4 outside the box on each side and one in the middle. Look at their coordinates. See a pattern? – doug Sep 03 '21 at 02:13

2 Answers2

1
v::Mat BoxValues = cv::Mat::zeros(detected_objects[i].rect.Height, detected_objects[i].rect.Width,type());
BoxValues = osrc(cv::Range(int(detected_objects[i].rect.y()),int(detected_objects[i].rect.y())+detected_objects[i].rect.Height),cv::Range(int(detected_objects[i].rect.x()),int(detected_objects[i].rect.x())+detected_objects[i].rect.Wigth));

Result BoxValues will holds the Inside the Bounding box Pixcel Values. Regarding the coordinate hope we can get by the cv::Range.

tamil
  • 99
  • 8
-1

I found a solution!

Here's the part of the code to extract the pixels values and coordinates inside of yolo bounding boxes :

 for (i = 0; i < detected_objects.size(); ++i) {
    xmin = detected_objects[i].rect.x;
    ymin = detected_objects[i].rect.y;
    width = detected_objects[i].rect.width;
    height = detected_objects[i].rect.height;
    xmax = xmin + width;
    ymax = ymin + height;
    Rect rect(xmin, ymin, width, height); //The upper left coordinates (x, y) and the length (x) and width (y) of the rectangle
    cv::rectangle(osrc, rect, Scalar(200, 200, 10), 1, LINE_8, 0); // set rectangle color
    std::cout << "rect number:" << rect << "[" << i << "]" << "\n"; // print the number of detected objects

    for (x = xmin; x < xmax; x++)
    {
        for (y = ymin; y < ymax; y++)
        {
            // pixel_values = osrc.at<Vec3b>(x, y); // for 3 channels output
            // Scalar intensity = osrc.at<uchar>(y, x);
            Scalar intensity = osrc.at<uchar>(Point(x, y)); //get pixel intensity from one channel (Grayscale image)
            std::cout << "Pixel intensity: " << intensity <<  "[" << i << "]" << "\n";
            //allocate the array
            int** arr = new int* [xmax];
            for (int k = 0; k < xmax; k++)
                arr[k] = new int[ymax];

            // use the array
            cout << "coords: "<<"[" << x << "," << y << "]: \n ";
            //cout << arr[x][y] << endl;

            //deallocate the array
            for (int k = 0; k < xmax; k++)
                delete[] arr[k];
            delete[] arr;


            // std::cout << " \npixel: \n " << Point(x,y);
         //   pixelValue = GetPixel(hDC, pos.x, pos.y);
         //   std::cout << " \pixelValue: \n" << pixelValue;
        }
    }

}