Help Object detection and tracking with color space conversion and morph operators
I want to implement with boundingbox the opencv example code on the link:
https://www.opencv-srf.com/2010/09/object-detection-using-color-seperation.html
I need to conversion XF_8UC1 to XF_8UC3 to hsv2rgb
and I also get hanging error in the for loop and xf::cv::boundingbox:
pixel = imgOutput.read_float(i);
#include "xf_colordetect_config.h" static constexpr int __XF_DEPTH = (HEIGHT * WIDTH * (XF_PIXELWIDTH(IN_TYPE, NPC1)) / 8) / (INPUT_PTR_WIDTH / 8); static constexpr int __XF_DEPTH_OUT = (HEIGHT * WIDTH * (XF_PIXELWIDTH(OUT_TYPE, NPC1)) / 8) / (OUTPUT_PTR_WIDTH / 8); static constexpr int __XF_DEPTH_FILTER = (FILTER_SIZE * FILTER_SIZE); void colordetect_accel(ap_uint<INPUT_PTR_WIDTH>* img_in, ap_uint<OUTPUT_PTR_WIDTH>* img_out, int rows, int cols) { #pragma HLS INTERFACE m_axi port=img_in offset=slave bundle=gmem0 depth=__XF_DEPTH #pragma HLS INTERFACE m_axi port=img_out offset=slave bundle=gmem4 depth=__XF_DEPTH_OUT #pragma HLS INTERFACE s_axilite port=rows #pragma HLS INTERFACE s_axilite port=cols #pragma HLS INTERFACE s_axilite port=return // Initialize input and intermediary matrices xf::cv::Mat<IN_TYPE, HEIGHT, WIDTH, NPC1, XF_CV_DEPTH_IN_1> imgInput(rows, cols); xf::cv::Mat<IN_TYPE, HEIGHT, WIDTH, NPC1, XF_CV_DEPTH_RGB2HSV> rgb2hsv(rows, cols); xf::cv::Mat<OUT_TYPE, HEIGHT, WIDTH, NPC1, XF_CV_DEPTH_HELP_1> imgHelper1(rows, cols); xf::cv::Mat<OUT_TYPE, HEIGHT, WIDTH, NPC1, XF_CV_DEPTH_HELP_2> imgHelper2(rows, cols); xf::cv::Mat<OUT_TYPE, HEIGHT, WIDTH, NPC1, XF_CV_DEPTH_HELP_3> imgHelper3(rows, cols); xf::cv::Mat<OUT_TYPE, HEIGHT, WIDTH, NPC1, XF_CV_DEPTH_HELP_3> imgHelper4(rows, cols); xf::cv::Mat<OUT_TYPE, HEIGHT, WIDTH, NPC1, XF_CV_DEPTH_HELP_3> imgHelper5(rows, cols); xf::cv::Mat<OUT_TYPE, HEIGHT, WIDTH, NPC1, XF_CV_DEPTH_OUT_1> imgOutput(rows, cols); xf::cv::Mat<OUT_TYPE, HEIGHT, WIDTH, NPC1, XF_CV_DEPTH_OUT_1> imgOutputwithBox(rows, cols); unsigned char low_thresh[MAXCOLORS] = {170, 150, 60}; unsigned char high_thresh[MAXCOLORS] = {179, 255, 255}; unsigned char _kernel[FILTER_SIZE * FILTER_SIZE] = {1, 1, 1, 1, 1, 1, 1, 1, 1}; // 3x3 kernel for erosion/dilation // Load input image to xf::cv::Mat format xf::cv::Array2xfMat<INPUT_PTR_WIDTH, IN_TYPE, HEIGHT, WIDTH, NPC1, XF_CV_DEPTH_IN_1>(img_in, imgInput); // RGB to HSV conversion xf::cv::bgr2hsv<IN_TYPE, HEIGHT, WIDTH, NPC1, XF_CV_DEPTH_IN_1, XF_CV_DEPTH_RGB2HSV>(imgInput, rgb2hsv); // Color thresholding to detect specific colors xf::cv::colorthresholding<IN_TYPE, OUT_TYPE, MAXCOLORS, HEIGHT, WIDTH, NPC1, XF_CV_DEPTH_RGB2HSV, XF_CV_DEPTH_HELP_1>(rgb2hsv, imgHelper1, low_thresh, high_thresh); // Apply morphological operations (erode + dilate) xf::cv::erode<XF_BORDER_CONSTANT, OUT_TYPE, HEIGHT, WIDTH, XF_KERNEL_SHAPE, FILTER_SIZE, FILTER_SIZE, ITERATIONS, NPC1, XF_CV_DEPTH_HELP_1, XF_CV_DEPTH_HELP_2>(imgHelper1, imgHelper2, _kernel); xf::cv::dilate<XF_BORDER_CONSTANT, OUT_TYPE, HEIGHT, WIDTH, XF_KERNEL_SHAPE, FILTER_SIZE, FILTER_SIZE, ITERATIONS, NPC1, XF_CV_DEPTH_HELP_2, XF_CV_DEPTH_HELP_3>(imgHelper2, imgHelper3, _kernel); xf::cv::dilate<XF_BORDER_CONSTANT, OUT_TYPE, HEIGHT, WIDTH, XF_KERNEL_SHAPE, FILTER_SIZE, FILTER_SIZE, ITERATIONS, NPC1, XF_CV_DEPTH_HELP_2, XF_CV_DEPTH_HELP_3>(imgHelper3, imgHelper4, _kernel); xf::cv::erode<XF_BORDER_CONSTANT, OUT_TYPE, HEIGHT, WIDTH, XF_KERNEL_SHAPE, FILTER_SIZE, FILTER_SIZE, ITERATIONS, NPC1, XF_CV_DEPTH_HELP_1, XF_CV_DEPTH_HELP_2>(imgHelper4, imgOutput, _kernel); xf::cv::hsv2rgb<XF_8UC1, XF_8UC1, HEIGHT, WIDTH, NPC1, XF_CV_DEPTH_IN_1, XF_CV_DEPTH_IN_1>(imgOutput, imgOutputwithBox); // Calculate bounding box by iterating through imgThresholded //int min_x = cols, min_y = rows, max_x = 0, max_y = 0; /*for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { unsigned char pixel; pixel = imgOutput.read_float(i); if (pixel > 0) { // If pixel is part of the detected region // Update bounding box coordinates if (j < min_x) min_x = j; if (j > max_x) max_x = j; if (i < min_y) min_y = i; if (i > max_y) max_y = i; } } }*/ // Define the bounding box region as an xf::cv::Rect object /*xf::cv::Rect_<int> roi; roi.x = min_x; roi.y = min_y; roi.width = max_x - min_x + 1; roi.height = max_y - min_y + 1;*/ // Define the bounding box color (e.g., green) //xf::cv::Scalar<4, unsigned char> color(0, 255, 0, 255); // RGBA format, green color // Draw bounding box on the thresholded image //xf::cv::boundingbox<OUT_TYPE, HEIGHT, WIDTH, 1, NPC1, XF_CV_DEPTH_HELP_1>(imgOutput, &roi, &color, 1); // Step 5: Convert processed data to output array xf::cv::xfMat2Array<OUTPUT_PTR_WIDTH, OUT_TYPE, HEIGHT, WIDTH, NPC1, XF_CV_DEPTH_OUT_1>(imgOutput, img_out); }