亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將RGB圖像轉換為灰度,但保留一種顏色?

如何將RGB圖像轉換為灰度,但保留一種顏色?

如何將RGB圖像轉換為灰度,但保留一種顏色?我試圖創造一個類似于罪惡之城或其他電影,他們刪除所有的顏色,但一個從圖像中刪除。我有一個RGB圖像,我想轉換為灰度,但我想保留一種顏色。這是我的照片:我想保留紅色。剩下的應該是灰階的。到目前為止,這就是我的代碼輸出的內容(您可以看到這些區域是正確的,我不知道為什么它們是白色的,而不是紅色的):到目前為止,這是我的代碼:filename = 'roses.jpg';[cdata,map] = imread( filename );% convert to RGB if it is indexed imageif ~isempty( map )     cdata = idx2rgb( cdata, map ); end%imtool('roses.jpg');imWidth = 685;imHeight = 428;% RGB ranges of a color we want to keepredRange     = [140 255];greenRange = [0 40];blueRange = [0 40];% RGB values we don't want to convert to grayscaleredToKeep =      zeros(imHeight, imWidth);greenToKeep = zeros(imHeight, imWidth);blueToKeep = zeros(imHeight, imWidth);for x=1:imWidth     for y=1:imHeight         red = cdata( y, x, 1 );         green = cdata( y, x, 2 );         blue = cdata( y, x, 3 );         if (red >= redRange(1) && red <= redRange(2) && green >= greenRange(1) && green <= greenRange(2) && blue >= blueRange(1) &         & blue <= blueRange(2))             redToKeep( y, x ) = red;             greenToKeep( y, x ) = green;             blueToKeep( y, x ) = blue;         else             redToKeep( y, x ) = 999;             greenToKeep( y, x ) = 999;             blueToKeep( y, x ) = 999;         end     end end im = rgb2gray(cdata);[X, map] = gray2ind(im);im = ind2rgb(X, map);for x=1:imWidth     for y=1:imHeight         if (redToKeep( y, x ) < 999)             im( y, x, 1 ) = 240;         end         if (greenToKeep( y, x ) < 999)             im( y, x, 2 ) = greenToKeep( y, x );         end         if (blueToKeep( y, x ) < 999)             im( y, x, 3 ) = blueToKeep( y, x );         end     end end imshow(im);
查看完整描述

3 回答

?
慕仙森

TA貢獻1827條經驗 獲得超8個贊

figurepic = imread('EcyOd.jpg');for mm = 1:size(pic,1)
    for nn = 1:size(pic,2)
        if pic(mm,nn,1) < 80 || pic(mm,nn,2) > 80 || pic(mm,nn,3) > 100
            gsc = 0.3*pic(mm,nn,1) + 0.59*pic(mm,nn,2) + 0.11*pic(mm,nn,3);
            pic(mm,nn,:) = [gsc gsc gsc];
        end
    endendimshow(pic)


查看完整回答
反對 回復 2019-07-10
?
小怪獸愛吃肉

TA貢獻1852條經驗 獲得超1個贊

一個大大提高圖像質量的選擇是轉換到一個不同的顏色空間,以便更容易地選擇您的顏色。特別是,HSV顏色空間定義像素顏色的色調(顏色)、飽和度(顏色的數量)和值(顏色的亮度)。

例如,可以使用以下函數將rgb圖像轉換為hsv空間。rgb2hsv,查找具有要定義為“非紅色”顏色的像素(例如,20到340度),將這些像素的飽和度設置為0(因此它們是灰度),然后使用該函數將圖像轉換回rgb空間。hsv2rgb:

cdata = imread('EcyOd.jpg');       % Load imagehsvImage = rgb2hsv(cdata);         
% Convert the image to HSV spacehPlane = 360.*hsvImage(:, :, 1);   % Get the hue plane scaled from 0 to 360sPlane = hsvImage(:, :, 2);        
% Get the saturation planenonRedIndex = (hPlane > 20) & ...  % Select "non-red" pixels
              (hPlane < 340);sPlane(nonRedIndex) = 0;           
              % Set the selected pixel saturations to 0hsvImage(:, :, 2) = sPlane;        
              % Update the saturation planergbImage = hsv2rgb(hsvImage);      
              % Convert the image back to RGB space

下面是產生的圖像:

注意,與塞勒斯的溶液,你可以很容易地保持花上淡粉色的色調。還請注意,褐色色調的莖和地面也消失了。

關于根據圖像顏色屬性從圖像中選擇對象的一個很酷的示例,您可以查看SteveEddins的博客文章。兩個伙伴它描述了一種來自MathWorks的BrettShoelson的解決方案,用于從圖像中提取一個“朋友”。


關于選擇顏色范圍的注意事項.。

另外一件可以幫助你選擇顏色范圍的事情是查看顏色的直方圖。hPlane)出現在HSV圖像的像素中。下面是一個使用函數的示例histc(或建議histcounts(如有的話)和bar:

binEdges = 0:360;    % Edges of histogram binshFigure = figure();  
% New figure% Bin pixel hues and plot histogram:if verLessThan('matlab', '8.4')
  N = histc(hPlane(:), binEdges);  % Use histc in older versions
  hBar = bar(binEdges(1:end-1), N(1:end-1), 'histc');else
  N = histcounts(hPlane(:), binEdges);
  hBar = bar(binEdges(1:end-1), N, 'histc');endset(hBar, 'CData', 1:360, ...            % Change the color of the bars using
          'CDataMapping', 'direct', ...  %   indexed color mapping (360 colors)
          'EdgeColor', 'none');          %   and remove edge coloringcolormap(hsv(360));                     
           % Change to an HSV color map with 360 pointsaxis([0 360 0 max(N)]);                 
            % Change the axes limitsset(gca, 'Color', 'k');                 
             % Change the axes background colorset(hFigure, 'Pos', [50 400 560 200]);   
             % Change the figure sizexlabel('HSV hue (in degrees)');          
             % Add an x labelylabel('Bin counts');                   
              % Add a y label

下面是得到的像素顏色直方圖:

注意原始圖像是如何包含紅色、綠色和黃色像素的(有幾個橙色像素)。幾乎沒有青色、藍色、靛藍色或品紅色像素。還請注意,我在上面選擇的范圍(20到340度)很好地排除了大部分不是兩端兩個大的紅色星系團的一部分的東西。


查看完整回答
反對 回復 2019-07-10
?
開滿天機

TA貢獻1786條經驗 獲得超13個贊

我真的不知道MATLAB是如何工作的,所以我不能對代碼進行評論,但這可能有助于解釋RGB顏色是如何工作的。

當使用RGB顏色時,可以通過確保R、G和B的值都相同來確定灰度。因此,基本上您想要做的是檢測一個像素是否是紅色的,當不只是使R、G和B相同時(您可以使用3的平均值來表示一個基本的結果)。

更難的部分是如何檢測一個像素是否實際上是紅色的,您不能僅僅檢查一個像素在R值中是否高,因為它仍然可以是另一種顏色,而低的R值只意味著更深的紅色。

所以您可以這樣做:(我沒有MATLAB,所以假設語法):

red = cdata( y, x, 1 );
green = cdata( y, x, 2 );
blue = cdata(y, x, 3);

if (red < (blue * 1.4) || red < (green * 1.4) )
{
    avg = (red + green + blue) / 3;
    cdata(y, x, 1) = avg;
    cdata(y, x, 2) = avg;
    cdata(y, x, 3) = avg;
}

也許有更好的方法來檢測紅色和平均灰度,但這是一個開始;)


查看完整回答
反對 回復 2019-07-10
  • 3 回答
  • 0 關注
  • 991 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號