首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >利用matlab将直方图格式转换为c#?

利用matlab将直方图格式转换为c#?
EN

Stack Overflow用户
提问于 2017-12-27 02:07:38
回答 1查看 159关注 0票数 0

如何将这个matlab代码转换为AForge.net+c#代码?

代码语言:javascript
复制
cdf1 = cumsum(hist1) / numel(aa); 

我发现Histogram.cumulative方法存在于Accord.net中。但我不知道怎么用。

请教如何转换。

代码语言:javascript
复制
% Histogram Matching
%

clear
clc
close all

pkg load image

% 이미지 로딩
aa=imread('2.bmp');
ref=imread('ref2.png');

figure(1); imshow(aa); colormap(gray)
figure(2); imshow(ref); colormap(gray)

M = zeros(256,1,'uint8'); % Store mapping - Cast to uint8 to respect data type
hist1 = imhist(aa); % Compute histograms
hist2 = imhist(ref);

cdf1 = cumsum(hist1) / numel(aa); % Compute CDFs
cdf2 = cumsum(hist2) / numel(ref);

% Compute the mapping
for idx = 1 : 256
[~,ind] = min(abs(cdf1(idx) - cdf2));


M(idx) = ind-1;
end

% Now apply the mapping to get first image to make
% the image look like the distribution of the second image
out = M(double(aa)+1);

figure(3); imshow(out); colormap(gray)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-27 02:55:19

实际上,我对Accord.NET不太了解,但是阅读文档,我认为ImageStatistics类就是您要找的(参考在这里)。问题是,它不能为图像建立一个单一的直方图,你必须自己去做。Matlab中的imhist只合并这三个通道,然后计数整个像素出现的情况,所以您应该这样做:

代码语言:javascript
复制
Bitmap image = new Bitmap(@"C:\Path\To\Image.bmp");

ImageStatistics statistics = new ImageStatistics(image);
Double imagePixels = (Double)statistics.PixelsCount;

Int32[] histR = statistics.Red.Values.ToArray();
Int32[] histG = statistics.Green.Values.ToArray();
Int32[] histB = statistics.Blue.Values.ToArray();
Int32[] histImage = new Int32[256];

for (Int32 i = 0; i < 256; ++i)
    histImage[i] = histR[i] + histG[i] + histB[i];

Double cdf = new Double[256];
cdf[0] = (Double)histImage[0];

for (Int32 i = 1; i < 256; ++i)
    cdf[i] = (Double)(cdf[i] + cdf[i - 1]);

for (Int32 i = 0; i < 256; ++i)
    cdf[i] = cdf[i] / imagePixels;

C#中,可以从RGB信道值构建RGB值,如下所示:

代码语言:javascript
复制
public static int ChannelsToRGB(Int32 red, Int32 green, Int32 blue)
{
    return ((red << 0) | (green << 8) | (blue << 16));
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47985519

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档