Find the peak index of the row and column on the binary map (C++)

insert image description here
insert image description here

/**
 * @brief 区域生长算法,输入图像应为灰度图像
 * @param srcImage 区域生长的源图像
 * @param pt 区域生长点
 * @param ch1Thres 通道的生长限制阈值,临近像素符合±chxThres范围内才能进行生长
 * @param ch1LowerBind 通道的最小值阈值
 * @param ch1UpperBind 通道的最大值阈值,在这个范围外即使临近像素符合±chxThres也不能生长
 * @return 生成的区域图像(二值类型)
 */
Mat RegionGrow(Mat srcImage, Point pt, int ch1Thres,int ch1LowerBind=0,int ch1UpperBind=255)
{
    
    
    Point pToGrowing;                       //待生长点位置
    int pGrowValue = 0;                             //待生长点灰度值
    Scalar pSrcValue = 0;                               //生长起点灰度值
    Scalar pCurValue = 0;                               //当前生长点灰度值
    Mat growImage = Mat::zeros(srcImage.size(), CV_8UC1);   //创建一个空白区域,填充为黑色
    //生长方向顺序数据
    int DIR[8][2] = {
    
    {
    
    -1,-1}, {
    
    0,-1}, {
    
    1,-1}, {
    
    1,0}, {
    
    1,1}, {
    
    0,1}, {
    
    -1,1}, {
    
    -1,0}};
    vector<Point> growPtVector;                     //生长点栈
    growPtVector.push_back(pt);                         //将生长点压入栈中
    growImage.at<uchar>(pt.y, pt.x) = 1.0;              //标记生长点
    pSrcValue = srcImage.at<uchar>(pt.y, pt.x);         //记录生长点的灰度值

    while (!growPtVector.empty())                       //生长栈不为空则生长
    {
    
    
        pt = growPtVector.back();                       //取出一个生长点
        growPtVector.pop_back();

        //分别对八个方向上的点进行生长
        for (int i = 0; i<9; ++i)
        {
    
    
            pToGrowing.x = pt.x + DIR[i][0];
            pToGrowing.y = pt.y + DIR[i][1];
            //检查是否是边缘点
            if (pToGrowing.x < 0 || pToGrowing.y < 0 ||
                    pToGrowing.x > (srcImage.cols-1) || (pToGrowing.y > srcImage.rows -1))
                continue;

            pGrowValue = growImage.at<uchar>(pToGrowing.y, pToGrowing.x);       //当前待生长点的灰度值
            pSrcValue = srcImage.at<uchar>(pt.y, pt.x);
            if (pGrowValue == 0)                    //如果标记点还没有被生长
            {
    
    
                pCurValue = srcImage.at<uchar>(pToGrowing.y, pToGrowing.x);
                if(pCurValue[0] <= ch1UpperBind && pCurValue[0] >= ch1LowerBind )
                {
    
    
                    if (abs(pSrcValue[0] - pCurValue[0]) < ch1Thres )                   //在阈值范围内则生长
                    {
    
    
                        growImage.at<uchar>(pToGrowing.y, pToGrowing.x) = 1.0;      //标记为白色
                        growPtVector.push_back(pToGrowing);                 //将下一个生长点压入栈中
                    }
                }
            }
        }
    }
    return growImage.clone();
}


int main()
{
    
    
	// 读入图像
	const string img_path = "/mnt/d/projects/picking_point_cpp/depth.png";
	Mat img = imread(img_path, 0);
	Mat img_gray;
	//用于显示的两个深拷贝
	Mat out_img;
	cv::cvtColor(img, out_img, COLOR_GRAY2BGR);
	if (img.empty())
	{
    
    
		cout << "img not exists!";
		return -1;
	}
	cv::Mat img_area_grow=Mat::zeros(img.size(), CV_64FC1); //区域生长结果
	// cv::cornerHarris(img_gray, harris, 10, 5, 0.24);
	std::stack<cv::Point2f> seeds;
	Point2f seed;

	/*add seeding*/
	seed.x = float(160);
	seed.y = float(400);
	seeds.push(seed);
	
	int thres = 4;   //区域生长条件
	cv::Point seedPoint(160, 400);
	// AreaGrow(img, img_area_grow,flag, seeds,thres);
	img_area_grow = RegionGrow(img, seedPoint, thres);

	cv::imshow("img_area_grow", img_area_grow*255); //区域生长结果
	waitKey(0);

	std::vector<float> colSumList;
	for (int i = 0; i < img_area_grow.cols; i++)
	{
    
    
		float colSum = 0.0;
		colSumList.push_back(colSum);
	}

	for (int j = 0; j < img_area_grow.rows; j++)
	{
    
    
		uchar* ucPxielCol = img_area_grow.data + j * img_area_grow.step;
		for (int i = 0; i < img_area_grow.cols; i++)
		{
    
    
			colSumList[i] += ucPxielCol[i];
		}
	}	

	int maxIndexCol = std::distance(colSumList.begin(), std::max_element(colSumList.begin(), colSumList.end()));
	std::cout << "rows :" << img_area_grow.rows << " cols: " << img_area_grow.cols << endl;
    std::cout << "Maximum col value in the vector is at index " << maxIndexCol  << std::endl;

	std::vector<float> rowSumList;
	for (int j = 0; j < img_area_grow.rows; j++)
	{
    
    
		float rowSum = 0.0;
		uchar* ucPixelrow = img_area_grow.data + j * img_area_grow.step;
		for (int i = 0; i < img_area_grow.cols; i++)
		{
    
    
			rowSum += ucPixelrow[i];
		}
		rowSumList.push_back(rowSum);
		// std::cout << "row index: " << j << ", value :" << rowSum << endl;
		rowSum = 0;

	}	
	int maxIndexRow = std::distance(rowSumList.begin(), std::max_element(rowSumList.begin(), rowSumList.end()));
    std::cout << "Maximum row value in the vector is at index " << maxIndexRow << std::endl;

	cv::line(out_img, Point(maxIndexCol, 0), Point(maxIndexCol, img_area_grow.rows), Scalar(0, 0, 255), 2);
	cv::line(out_img, Point(0, maxIndexRow), Point(img_area_grow.cols, maxIndexRow), Scalar(0, 0, 255), 2);

	cv::imshow("Line", out_img); //区域生长结果
	waitKey(0);

}

Guess you like

Origin blog.csdn.net/weixin_42990464/article/details/130581852