matlab将灰度图转化为彩图(已验证)

您所在的位置:网站首页 灰度图转化为假彩色matlab matlab将灰度图转化为彩图(已验证)

matlab将灰度图转化为彩图(已验证)

2024-05-30 21:27| 来源: 网络整理| 查看: 265

转载于:http://blog.163.com/yuyang_tech/blog/static/216050083201303011153841/

Grayscale to RGB 很多时候,当我们在研究彩色图像的时候,我们从灰度空间,也就是亮度空间就能完成。也就是说,通过灰度表示,我们就可以获取图像的大部分信息。将彩色通道进行线性组合形成灰度通道,这是图像处理里面一个很常规的做法,在matlab工具箱中有rgb2gray这个函数可以完成。最近,在matlab的文件分享网站,有人发布了一个代码可以反过来变换图像。 文件要求提供一个灰度图像,以及一个作为调色板的任意彩色图像,然后,这个名为 gray2rgb的函数就可以创建提供的灰度图像的彩色版本,这个彩图用到第二幅图像的颜色。看看效果:

figure subplot(2,2,1) imshow('liftingbody.png'); subplot(2,2,2) imshow('gantrycrane.png'); colorIm = gray2rgb('liftingbody.png','gantrycrane.png'); subplot(2,2,3.5) imshow(colorIm) 12345678

这里写图片描述

很明显,gray2rgb这个文件对于很多人是有用的,因为它已经在文件分享里面存在好几年了,而且每个月都有接近100次下载。而且评价也不错。这个函数的缺点就是比较费时间。 至于这个函数的实现原理,从代码看,首先函数将灰度图像扩展到三通道(复制通道),然后将两张图像转为yCbCr空间。最后,通过一个二重循环来逐像素生成结果图。这显然是很耗时间的。我觉得应该可以找到更快的实现方法。

这里写图片描述

function R=gray2rgb(img1,img2) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % This Program converts a gray image ro RGB image based on the colors of the destination image. The better the destination image match with the source gray image, the better the coloring will be. The program takes some time as the searching time is high. You can decrease the searching time by taking only samples from the used color image but quality may decrease. U can use jittered sampling for improving running speed. % % You can use also use the attahed test images, Use the following combinations for better result nature1.jpg(as img1) and nature2.jpg(as img2) or test1.jpg(as img1) and test2.jpg (as img2) % % Usage: gray2rgb('nature1.jpg','nature2.jpg'); % % Authors : Jeny Rajan , Chandrashekar P.S % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % img1 - Source Image (gray image) % img2 - Selected color image for coloring the gray image. tic clc; warning off; imt=imread(img1); ims=imread(img2); [sx sy sz]=size(imt); [tx ty tz]=size(ims); if sz~=1 imt=rgb2gray(imt); end if tz~=3 disp ('img2 must be a color image (not indexed)'); else imt(:,:,2)=imt(:,:,1); imt(:,:,3)=imt(:,:,1); % Converting to ycbcr color space nspace1=rgb2ycbcr(ims); nspace2= rgb2ycbcr(imt); ms=double(nspace1(:,:,1)); mt=double(nspace2(:,:,1)); m1=max(max(ms)); m2=min(min(ms)); m3=max(max(mt)); m4=min(min(mt)); d1=m1-m2; d2=m3-m4; % Normalization dx1=ms; dx2=mt; dx1=(dx1*255)/(255-d1); dx2=(dx2*255)/(255-d2); [mx,my,mz]=size(dx2); %Luminance Comparison disp('Please wait..................'); for i=1:mx for j=1:my iy=dx2(i,j); tmp=abs(dx1-iy); ck=min(min(tmp)); [r,c] = find(tmp==ck); ck=isempty(r); if (ck~=1) nimage(i,j,2)=nspace1(r(1),c(1),2); nimage(i,j,3)=nspace1(r(1),c(1),3); nimage(i,j,1)=nspace2(i,j,1); end end end rslt=ycbcr2rgb(nimage) figure,imshow(uint8(imt)); figure,imshow(uint8(rslt)); R=uint8(rslt); toc end 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3