C++ OpenCV像素强度

C++ OpenCV像素强度,c++,opencv,C++,Opencv,我只是在寻找OpenCV坐标系和像素强度系统背后的解释。我正在使用for循环来查找屏幕上最密集的像素(我知道我可以使用minMaxLoc来查找它,但是,我的代码也将被更改以查找最左边的像素,因此需要这个for循环) for(int i=0;iHeadIntenVal)//如果新强度高于旧强度,则将头部更改为新点并再次运行。 { //coutMat.at函数需要参数行、列顺序。 首先是Y,然后是X int imgHight = grayImg.rows; int imgWidth = grayIm

我只是在寻找OpenCV坐标系和像素强度系统背后的解释。我正在使用for循环来查找屏幕上最密集的像素(我知道我可以使用minMaxLoc来查找它,但是,我的代码也将被更改以查找最左边的像素,因此需要这个for循环)

for(int i=0;iLowerBounds)//比较下限(最大像素强度的80%)
{
if(newIntenVal>HeadIntenVal)//如果新强度高于旧强度,则将头部更改为新点并再次运行。
{

//coutMat.at函数需要参数行、列顺序。 首先是Y,然后是X

int imgHight = grayImg.rows;
int imgWidth = grayImg.cols;
int maxX = 0, maxY = 0;
uint8_t grayMax = grayImg.at<uint8_t>(maxY,maxX);

for (int i = 0; i < imgHight; i++) //Going through the height or Y axis.
{                                   // The height and width are equal the image is a square
    for (int j = 0; j < imgWidth; j++) //  Going through the width or X axis. 
    {
        uint8_t newInten = grayImg.at<uchar>(i, j); // This uses the X and Y to find a new point
        if (newInten > grayMax)
        {
            grayMax = newInten;
            maxX = j;
            maxY = i;
        }
    }
}
int imgHight=grayimgrows;
int imgWidth=灰色img.cols;
int maxX=0,maxY=0;
uint8_t grayMax=灰度最大值(最大值,最大值);
对于(int i=0;igrayMax)
{
grayMax=newInten;
maxX=j;
maxY=i;
}
}
}
还对代码进行了一些修改,使其更快。
但这不是最快的选择。

在添加建议的修复后,我按照通常绘制圆圈的方式交换了X和Y,我还需要记住将maxX与Leftside.Y进行比较。结果现在与预期一致。谢谢大家!

屏幕截图链接似乎没有显示您所描述的内容-可以吗尝试使用图像上载按钮将其上载到Stack Overflow?我无法上载图像,因为没有足够的rep:/一直说我需要10个rep来直接发布图像。我检查了链接,如果您放大,它会正确显示,如果我更改圆圈颜色,可能会更容易看到。圆圈已更改为白色,图片已显示应该在该区域进行更大的缩放!任何帮助都将不胜感激。这完全是低效的-请记住HeadInten`Paralled
Head
,而不是每次都重新计算它。我对该库完全陌生,所以为了公平起见,这只是一个散列,但我现在可以更改它。按照建议更改它,结果是一样的,我要走了谢谢你的帮助,虽然我不知道这是一个YX系统
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;

int main()
{
    double IBKG, IMAX, TC, LowerBounds; // IBKG is Image Background Level 
    Intensity, IMAX is Maximum Image Intensity, TC is comet theshold
    Point IBKG_LOC, IMAX_LOC; // These are the respective locations on the 
    image, head is to find the head of the comet

    Mat img = imread("Test1.png");
    Mat grayImg;
    int imgHight = img.rows;
    int imgWidth = img.cols;
    cout << imgHight << " " << imgWidth << endl;

    cvtColor(img, grayImg, CV_RGB2GRAY);
    minMaxLoc(grayImg, &IBKG, &IMAX, &IBKG_LOC, &IMAX_LOC);
    cout << IMAX_LOC << endl;
    TC = (IBKG + IMAX) / 2;
    LowerBounds = IMAX * 0.8;
    cout << LowerBounds << endl;
    uint8_t newInten;
    int maxX = 0, maxY = 0;
    uint8_t grayMax = grayImg.at<uint8_t>(maxY, maxX);
    for (int i = 0; i < imgHight; i++) //Going through the height or Y axis.
    {                                   // The height and width are equal the image is a square
        for (int j = 0; j < imgWidth; j++) //  Going through the width or X axis. 
        {
            uint8_t newInten = grayImg.at<uchar>(i,j); // This uses the X and Y to find a new point

            if (newInten > LowerBounds) //Compaired to lower bounds (80% of max pixel intensity)
            {

                if (newInten > grayMax) // If the new intensity is higher than old then change head to new point and run again.
                {
                grayMax = newInten;
                maxX = j;
                maxY = i;
                }
            }
        }
    }

    Point LeftSide;
    bool leftSideFlag = false;
    for (int i = maxX; i > 0; i--)
    {
        newInten = grayImg.at<uchar>(maxY, i);

        if (!leftSideFlag)
        {
            if (newInten < TC)
            {
                LeftSide = { maxY, i};
                leftSideFlag = true;
                //i = 1;
            }
        }
    }

    int CircleRadius = maxX - LeftSide.x;

    circle(img, Point(maxY, maxX), 10, Scalar(255, 255, 255));

    cout << IBKG <<" " << IMAX << " " << IBKG_LOC << " "  << IMAX_LOC << " " << TC << endl;
    namedWindow("Image", WINDOW_NORMAL);
    namedWindow("Gray Image", WINDOW_NORMAL);
    imshow("Image", img);
    imshow("Gray Image", grayImg);

    waitKey(0);
    return 0;
int imgHight = grayImg.rows;
int imgWidth = grayImg.cols;
int maxX = 0, maxY = 0;
uint8_t grayMax = grayImg.at<uint8_t>(maxY,maxX);

for (int i = 0; i < imgHight; i++) //Going through the height or Y axis.
{                                   // The height and width are equal the image is a square
    for (int j = 0; j < imgWidth; j++) //  Going through the width or X axis. 
    {
        uint8_t newInten = grayImg.at<uchar>(i, j); // This uses the X and Y to find a new point
        if (newInten > grayMax)
        {
            grayMax = newInten;
            maxX = j;
            maxY = i;
        }
    }
}