如何使用MATLAB计算图像中曲线下的面积? [英] How do I calculate the area under a curve in an image with MATLAB?

查看:1531
本文介绍了如何使用MATLAB计算图像中曲线下的面积?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

alt text http://internationalpropertiesregistry.com/ Server / showFile.php?file =%2FUpload%2Fstatistics.gifc49ca28823a561a41d09ef9adbb5e0c5.gif

x轴的单位是小时( h ),共计24小时。

The unit of x-axis is hours (h), and there are 24 hours in total.

y轴的单位为百万( m )。

The unit of y-axis is millions (m).

如何计算图像中红色曲线下的面积,单位为 m * h

How do I calculate the area under the red curve in the image in units of m*h?

重要更新

只有图片很容易可用(不是数据),我想以编程方式计算区域

Only the image is readily available (not the data), and I want to calculate the area programmatically.

推荐答案

难度创建一个完全自动化的解决方案是,它需要您硬盘编码到您的解决方案有关您正在输入的输入图像的某些假设g要处理。如果这些假设不适用于所有您可能遇到的潜在图像,那么全自动解决方案将无法提供值得信赖的结果,并尝试扩展全自动解决方案以处理所有可能的输入都可能导致它膨胀成一个难以理解和复杂的代码混乱。

The difficulty with creating a fully-automated solution is that it would require you to hardcode into your solution certain assumptions about the input images you are going to process. If these assumptions don't hold for all the potential images you may come across, the fully-automated solution won't give trustworthy results, and trying to extend the fully-automated solution to handle all possible inputs will likely cause it to bloat into an incomprehensible and complicated mess of code.

如果对输入图像的功能变化有疑问, 雅各布的解决方案一些用户交互通常是最好的。如果您某些输入图像的功能遵循严格的规则,则可以考虑使用自动解决方案。

When in doubt about the variability in features of your input images, a solution like Jacob's with some user interaction is generally best. If you can be certain that the features of your input images follow a strict set of rules, then an automated solution can be considered.

As例如,下面是我编写的一些自动代码,用于近似图表中红色曲线下的区域。由于我使用上面的图表作为指南,因此必须满足一些条件才能工作:

As an example, below is some automated code I wrote to approximate the area under the red curve in your graph. Since I used the above graph as a guide, there are a number of conditions that must be met for it to work:


  • 红色像素绘制线的必须在图像中唯一地描述为包含等于0的绿色和蓝色分量以及等于1的红色分量。

  • 网格线的绿色像素必须是唯一的在图像中描述为包含小于1的红色和蓝色分量以及等于1的绿色分量。

  • 轴线的蓝色像素必须在图像中唯一描述为包含红色和绿色成分等于0,蓝色成分等于1.

  • 网格和轴线必须始终在水平或垂直方向上精确对齐。

  • 网格线的长度必须超过图像宽度和高度的一半。

  • x轴必须是图像中最长的水平蓝线。

  • 网格线必须alwa是1像素厚。

  • The red pixels of the plotted line must be uniquely described in the image as containing green and blue color components equal to 0 and red color components equal to 1.
  • The green pixels of the grid lines must be uniquely described in the image as containing red and blue color components less than 1 and green color components equal to 1.
  • The blue pixels of the axes lines must be uniquely described in the image as containing red and green color components equal to 0 and blue color components equal to 1.
  • The grid and axis lines must always be exactly aligned in a horizontal or vertical direction.
  • The length of the grid lines must span well over half the width and height of the image.
  • The x axis must be the longest horizontal blue line in the image.
  • The grid lines must always be 1 pixel thick.

根据输入图像的上述条件,以下代码可用于估算下面的区域没有用户输入的红色曲线:

Subject to the above conditions on the input image, the following code can be used to approximate the area under the red curve without user input:

[img,map] = imread('original_chart.gif');  %# Read the indexed image
[r,c] = size(img);                         %# Get the image size

redIndex = find((map(:,1) == 1) & ...    %# Find the red index value
                (map(:,2) == 0) & ...
                (map(:,3) == 0))-1;
greenIndex = find((map(:,1) < 1) & ...   %# Find the green index value
                  (map(:,2) == 1) & ...
                  (map(:,3) < 1))-1;
blueIndex = find((map(:,1) == 0) & ...   %# Find the blue index value
                 (map(:,2) == 0) & ...
                 (map(:,3) == 1))-1;

redLine = (img == redIndex);      %# A binary image to locate the red line
greenLine = (img == greenIndex);  %# A binary image to locate the grid lines
blueLine = (img == blueIndex);    %# A binary image to locate the axes lines

w = mean(diff(find(sum(greenLine,1) > r/2)));  %# Compute unit square width
h = mean(diff(find(sum(greenLine,2) > c/2)));  %# Compute unit square height
squareArea = w*h;                              %# Compute unit square area

[maxValue,maxIndex] = max(redLine);          %# Find top edge of red line
x = find(maxValue > 0);                      %# Find x coordinates of red line
y = maxIndex(maxValue > 0);                  %# Find y coordinates of red line
[maxValue,maxIndex] = max(sum(blueLine,2));  %# Find row index of x axis
y = maxIndex-y;                              %# Zero the y coordinate
totalArea = trapz(x,y)/squareArea;           %# Compute the area under the curve

这给出了以下结果:

squareArea = 460.6 square pixels
totalArea = 169.35 m*h



说明:

我将详细介绍这些步骤参与计算 w

I'll elaborate more about the steps involved in computing w:


  1. 二进制图像 greenLine 使用函数 SUM ,给出 1-by-c 向量,其中每个元素是图像每列中有多少网格线像素的计数。

  2. 此向量的元素大于 r / 2 (图像中行数的一半)表示图像的列包含垂直网格线。使用 FIND 。

  3. 使用函数 DIFF 。这给出了一个包含网格线之间空格宽度(以像素为单位)的向量。

  4. 最后,函数 MEAN 用于计算图像中所有网格线之间的空间平均宽度。

  1. The binary image greenLine is summed along each column using the function SUM, giving a 1-by-c vector where each element is a count of how many grid line pixels are in each column of the image.
  2. The elements of this vector that are greater than r/2 (half the number of rows in the image) indicate columns of the image that contain a vertical grid line. The indices of these columns are found using the function FIND.
  3. The pairwise differences between these column indices are found using the function DIFF. This gives a vector containing the widths (in pixels) of the spaces between grid lines.
  4. Finally, the function MEAN is used to compute the mean width of the spaces between all the grid lines in the image.

当计算 h 时,唯一的区别是总和是沿着每一行执行的并且 r / 2 替换为 c / 2 (图像中列数的一半)。

When computing h, the only difference is that the sum is performed along each row and r/2 is replaced with c/2 (half the number of columns in the image).

这篇关于如何使用MATLAB计算图像中曲线下的面积?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆