Code explanation (bw)
조회 수: 5 (최근 30일)
이전 댓글 표시
Any one know what is the meaning of this coding?
imageOutR(bw2(:)~=0) = Ro(bw2(:)~=0);
imageOutG(bw2(:)~=0) = Go(bw2(:)~=0);
imageOutB(bw2(:)~=0) = Bo(bw2(:)~=0);
Dccc = cat(3,imageOutR,imageOutG,imageOutB);
how to explain this code?
imageOutR(bw2(:)~=0) = Ro(bw2(:)~=0);
댓글 수: 0
답변 (3개)
Image Analyst
2021년 11월 15일
It's someone (not a super skilled MATLAB programmer evidently) trying to take part of R, G, and B images that are defined by a mask (logical, binary image), bw2, and put them into different R, G, and B images, which they then concatenate into an RGB image called Dcc. It could be written simpler as:
imageOutR(bw2) = Ro(bw2); % Paste Red image inside bw2 onto imageOutR
imageOutG(bw2) = Go(bw2); % Paste Green image inside bw2 onto imageOutG
imageOutB(bw2) = Bo(bw2); % Paste Blue image inside bw2 onto imageOutB
% Concatenate individual color channels into a 3-D true color RGB image.
Dccc = cat(3,imageOutR,imageOutG,imageOutB);
댓글 수: 0
Awais Saeed
2021년 11월 15일
The (:) reshapes a matrix into a column vector.
imageOutR = [zeros(1,3); 2 5 13; 0 1 2; 1 3 5] % 4x4 matrix for instance
bw2 = randi([0 1],1,10)
% get indices of bw2 where non-zero elements resides
% get elements of imageOutR at those indices
imageOutR(bw2(:)~=0)
Ro = magic(4)
% replace elements of imageOutR with corresponding Ro's elements at those indices
imageOutR(bw2(:)~=0) = Ro(bw2(:)~=0)
cat() is for concatenation of arrays
댓글 수: 0
yanqi liu
2021년 11월 15일
sir,may be check it by some demo
clc; clear all; close all;
RGB = imread('football.jpg');
% 3 channel
Ro=RGB(:,:,1);Go=RGB(:,:,2);Bo=RGB(:,:,3);
% make mask
bw2 = im2bw(RGB);
% get mask data in image
imageOutR = zeros(size(Ro));imageOutG = zeros(size(Ro));imageOutB = zeros(size(Ro));
imageOutR(bw2(:)~=0) = Ro(bw2(:)~=0);
imageOutG(bw2(:)~=0) = Go(bw2(:)~=0);
imageOutB(bw2(:)~=0) = Bo(bw2(:)~=0);
Dccc = uint8(cat(3,imageOutR,imageOutG,imageOutB));
figure;
montage({RGB,bw2,Dccc}, 'Size', [1 3], 'BackgroundColor', 'r', 'BorderSize', [3 3])
댓글 수: 2
Image Analyst
2021년 11월 15일
Nice illustration/demo. Like I said in my answer, you do not need bw2(:)~=0. You can simply replace it by bw2
참고 항목
카테고리
Help Center 및 File Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!