简体   繁体   English

如何确定x轴像素范围的像素强度?

[英]How to determine pixel intensity with respect to pixel range in x-axis?

I want to see the distribution of a color with respect to image width. 我想看看相对于图像宽度的颜色分布。 That is, if a (black and white) image has width of 720 px, then I want to conclude that a specific range (eg pixels [500,720]) has more white color in compared to rest of the image. 也就是说,如果(黑白)图像的宽度为720像素,那么我想得出结论:与图像的其余部分相比,特定范围(例如像素[500,720])具有更多的白色。 What I thought is, I need a slice of the image of 720x1 px, then I need to check the values and distribute them wrt width of 720 px. 我想的是,我需要一片720x1像素的图像,然后我需要检查这些值并将它们分配为宽度为720像素。 But I don't know the way I can apply this in a suitable way? 但我不知道我能以合适的方式应用它的方式吗?

edit: I use OpenCV 4.0.0 with C++. 编辑:我使用OpenCV 4.0.0和C ++。

Example Case: In the first image, it is obvious that right hand side pixels are white. 示例案例:在第一张图片中,很明显右侧像素是白色的。 I want to get estimate coordinates of this dense line or zone. 我想获得这条密集线或区域的估计坐标。 The light pink zone is where I am interested in and the red borders are the range where I want to find it. 淡粉色区域是我感兴趣的地方,红色边框是我想要找到它的范围。

在此输入图像描述 在此输入图像描述

If you want to get minimum continious range of image columns which contain more white than the rest of the image, than you need first to calculate number of white pixels in each column. 如果要获得包含比图像其余部分更多白色的最小连续图像列范围,则需要先计算每列中的白色像素数。 Lets assume we have an image 720x500 (500 pixels high and 720 pixels wide). 让我们假设我们有一个720x500(500像素高,720像素宽)的图像。 Than you will get an array Arr of 720 elements that equal number of white pixels in each column (1x500) respectively. 比你将获得一个720个元素的数组Arr ,它们分别与每列(1x500)中的白色像素数相等。

const int Width = img.cols;
int* Arr = new int[Width];
for( int x = 0; x < Width; x++ ) {
    Arr[x] = 0;
    for( int y = 0; y < img.rows; y++ ) {
        if ( img.at<cv::Vec3b>(y,x) == cv::Vec3b(255,255,255) ) {
            Arr[x]++;
        }
    }
}

You need to find a minimum range [A;B] in this array that satisfies condition Sum(Arr[0 to A-1]) + Sum(Arr[B+1 to Width-1]) < Sum(Arr[A to B]). 你需要在这个数组中找到满足条件Sum(Arr [0到A-1])+ Sum(Arr [B + 1到Width-1])<Sum(Arr [A到A]的最小范围[A; B] B])。

// minimum range width is guaranteed to be less or equal to (Width/2 + 1)
int bestA = 0, minimumWidth = Width/2 + 1;
int total = RangeSum(Arr, 0, Width-1);
for (int i = 0; i < Width; i++) {
    for (int j = i; j < Width && j < i + minimumWidth; j++) {
        int rangeSum = RangeSum(Arr, i, j);
        if (rangeSum > total - rangeSum) { 
            bestA = i; 
            minimumWidth = j - i + 1; 
            break;
        }
    }
}

std::cout << "Most white minimum range - [" << bestA << ";" << bestA + minimumWidth - 1 << "]\n";

You can optimize the code if you precalculate sums for all [0; 如果你预先计算所有[0;]的总和,你可以优化代码。 i] ranges, i from 0 to Width - 1. Than you can calculate RangeSum(Arr, A, B) as PrecalculatedSums[B] - PrecalculatedSums[A] (in O(1) complexity). i]范围,i从0到宽度 - 1.你可以将RangeSum(Arr, A, B)PrecalculatedSums[B] - PrecalculatedSums[A] (在O(1)复杂度中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM