YUV420 图像加马赛克MATLAB

mosaic_strength = uint32(50);
width = uint32(1440);
height = uint32(1440);
face_x = uint32(601);
face_y = uint32(497);
face_width= uint32(500);
face_height = uint32(600);

fid = fopen('E:/Projects/matlab/yuv420.yuv','r');
A = fread(fid, height*width*1.5, 'uint8');
fclose(fid);
A = reshape(A,[ width height*1.5]);
A = rescale(A,0,1);
yuv420 = A';
% 处理Y
for h=face_y : mosaic_strength : face_y+face_height
    for w=face_x : mosaic_strength : face_x+face_width
        for i=h:h+mosaic_strength-1
            for j=w:w+mosaic_strength-1
                yuv420(i, j) = yuv420(h, w);
            end
        end
    end
end
% 处理U:跟排列方式有关
for h=face_y/4 : mosaic_strength/4 : (face_y+face_height)/4
    for w=face_x/2 : mosaic_strength/2 : (face_x+face_width)/2
        for i=h:h+mosaic_strength/4-1
            for j=w:w+mosaic_strength/2-1
                yuv420(i+height, j) = yuv420(h+height,w);
            end
        end
    end
    for w=(face_x+width)/2 : mosaic_strength/2 : (face_x+face_width+width)/2
        for i=h:h+mosaic_strength/4-1
            for j=w:w+mosaic_strength/2-1
                yuv420(i+height, j) = yuv420(h+height,w);
            end
        end
    end
end
% 处理V:跟排列方式有关
for h=face_y/4 : mosaic_strength/4 : (face_y+face_height)/4
    for w=face_x/2 : mosaic_strength/2 : (face_x+face_width)/2
        for i=h:h+mosaic_strength/4-1
            for j=w:w+mosaic_strength/2-1
                yuv420(i+height*5/4, j) = yuv420(h+height*5/4,w);
            end
        end
    end
    for w=(face_x+width)/2 : mosaic_strength/2 : (face_x+face_width+width)/2
        for i=h:h+mosaic_strength/4-1
            for j=w:w+mosaic_strength/2-1
                yuv420(i+height*5/4, j) = yuv420(h+height*5/4,w);
            end
        end
    end
end
% 显示yuv420加马赛克图像
figure, imshow(yuv420,[])
% yuv420转yuv444
y = yuv420(1:1440,:);
u=zeros(720,720);
v=zeros(720,720);
u1 = yuv420(1440+1:1440*1.25,1:720);
u2 = yuv420(1440+1:1440*1.25,720+1:end);
for i=1:2:720
    u(i,:) = u1(1+floor(i/2),:);
    if(i<=720)
    u(i+1,:) = u2(1+floor(i/2),:);
    end
end
v1 = yuv420(1440*1.25+1:1440*1.5,1:720);
v2 = yuv420(1440*1.25+1:1440*1.5,720+1:end);
for i=1:2:720
    v(i,:) = v1(1+floor(i/2),:);
    if(i<=720)
    v(i+1,:) = v2(1+floor(i/2),:);
    end
end
u = imresize(u, 2, 'bilinear');
v = imresize(v, 2, 'bilinear');
yuv444 = cat(3, y, u, v);
yuv444 = uint8(yuv444*256);
% yuv444转rgb
RGB = yuv2rgb(yuv444);
% 显示rgb加马赛克
figure,imshow(RGB)

% 查看加马赛克索引
% image = zeros(height,width);
% for h=face_y/2 : mosaic_strength/2 : (face_y+face_height)/2
%     for w=face_x/2 : mosaic_strength/2 : (face_x+face_width)/2
%         for i=h:h+mosaic_strength
%             for j=w:w+mosaic_strength
%                 image(i, j) = w + h*width/2;
%             end
%         end
%     end
% end
% for h=face_y/2 : (face_y+face_height)/2
%     for w=face_x/2 : (face_x+face_width)/2
%         image(h, w) = w - mod(w, mosaic_strength) + (h - mod(h, mosaic_strength)) * width/2;
%     end
% end


yuv444转rgb

function RGB = yuv2rgb(YUV)
    % 定义转换矩阵
    M = [1.164,  0.000,  1.596; ...
         1.164, -0.392, -0.813; ...
         1.164,  2.017,  0.000];

    % 提取Y, U, V分量
    Y = double(YUV(:, :, 1)) - 16;
    U = double(YUV(:, :, 2)) - 128;
    V = double(YUV(:, :, 3)) - 128;

    % 初始化RGB矩阵
    [height, width] = size(Y);
    RGB = zeros(height, width, 3);

    % 执行颜色空间转换
    for i = 1:height
        for j = 1:width
            RGB(i, j, :) = M * [Y(i, j); U(i, j); V(i, j)];
        end
    end

    % 限制RGB值在0到255之间
    RGB = max(min(RGB, 255), 0);
    RGB = rescale(RGB, 0, 1);
end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值