• Matlab强迫症产生的图像


    最近流行的网络迷恋的照片做头像,闲来无事,取matlab获取一个建设者,它可以产生包括0-9以及99+OCD。

    原理很easy,图叠加,这里为了降低文件,将数字图片保存在.mat二进制文件里。

    =====================================================================================================

    代码例如以下:

    clear,clc
    %% open image file
    [fn,pn,fi]=uigetfile('*.bmp;*.png;*.jpg;*.jpeg','choose image');
    img=imread([pn fn]);
    %% process image 
    width = size(img,1);%image width
    height = size(img,2);%image height
    minlength = min([width,height]);%min of width and height
    circle_d = floor(minlength*(1-0.618));%circle radius
    circle_x_end = circle_d;
    circle_y_start = height - circle_d + 1;
    %circle center
    circle_center_x = circle_x_end/2;
    circle_center_y = (circle_y_start+height)/2;
    %draw circle--------------------------------
    for i=1:circle_x_end
        for j=circle_y_start:height
            if((i-circle_center_x)^2+(j-circle_center_y)^2 <= (circle_d/2)^2)
                img(i,j,1) = 255;
                img(i,j,2) = 0;
                img(i,j,3) = 0;
            end
        end
    end
    % draw number-------------------------------
    load number.mat
    num = input('Input the number to add to the image:');
    while(num<0 || num>9)
        num = input('Input error,out of range(0-9),reinput the number:'); 
    end
    %-------------------------------------------
    %scale the nu7mber image
    var_name = ['number' num2str(num)];
    eval([var_name '=imresize(' var_name ',[circle_x_end,height - circle_y_start+1]);']);
    %sharp the image
    eval(['index = find(' var_name '>0);']);
    for k=1:length(index)
        eval([var_name '(index(k)) = 255;']);
    end
    %add the value to the 2nd and 3rd layer
    eval(['img(1:circle_x_end,circle_y_start:height,2) = img(1:circle_x_end,circle_y_start:height,2)+' var_name ';']);
    eval(['img(1:circle_x_end,circle_y_start:height,3) = img(1:circle_x_end,circle_y_start:height,3)+' var_name ';']);
    %% show image
    imshow(img);
    %% save image
    dotindex = max(find(fn=='.'));
    imwrite(img,[fn(1:dotindex-1) ' with number ' num2str(num) fn(dotindex:end)]);
    
    

    =====================================================================================================

    另外数字的二进制文件随代码一起上传在此(点击此处下载


    效果图例如以下:







    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    Linux面试题汇总答案
    VMWARE ESXI 虚拟硬盘的格式:精简置备、厚置备延迟置零、厚置备置零
    [Python基础知识]正则
    [代码评审1]代码评审
    [EF2]Sneak Preview: Persistence Ignorance and POCO in Entity Framework 4.0
    [EF1]POCOs(Plain Old C# Object)Entity Framework 4.x: POCOs入门
    [网站性能3]SqlServer中Profiler的使用
    [网站性能2]Asp.net平台下网站性能调优的实战方案
    [网站性能1]对.net系统架构改造的一点经验和教训
    2.1 python使用MongoDB 示例代码
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/4724512.html
Copyright © 2020-2023  润新知