基于MATLAB的图像色彩识别项目,彩色图像矩阵识别

一、项目背景

在本次IP Lab项目中,我们模拟了游戏"Lego's Life of George"的玩法。游戏要求玩家在屏幕短暂展示图像后,凭借记忆重建乐高结构。我们面临的任务是处理一系列存在各种缺陷的图像,像噪声干扰、旋转以及投影问题等,利用MATLAB开发程序对这些图像进行预处理,并准确生成代表16个方块颜色的字符串数组。项目选用了"images2"数据集开展图像处理工作。

二、方法与函数实现

(一)LoadImage函数

该函数用于处理图像输入,主要作用是将unit8格式的图像转换为double类型,这样做能提升后续MATLAB图像处理操作的精度。如果输入图像并非unit8格式,函数会给出错误提示。示例代码如下:

matlab 复制代码
function [ image] = LoadImage( filename )
    %function to load the image and convert it to type double
    image=imread(filename); %read the image file
    if isa(image,'uint8')%check if the image is type unit8
        image=double(image)/255; %change the image type to double
    else
        error('The image is of unknown type');%return error message if image is not type unit8
    end
end

(二)findCircles函数

findCircles函数以double类型图像作为输入,其核心功能是定位图像中圆形的坐标(圆心和半径),并借助MATLAB的viscircles命令在图像上直观标记出检测到的圆形。在实际测试中,该函数识别圆形的成功率高达100%。代码实现如下:

matlab 复制代码
function [centers,radii] = findCircles(image)
    [Centers,radii]= imfindcircles(image,[20 25],'ObjectPolarity','dark','Sensitivity',0.92,'Method','twostage'); %Detect and find the Circles with their centers and Radii.
    circle= viscircles(Centers,radii) %Draw the Cricles boundaries on the image. 
    centers=[Centers] %centers of the circles
    radii=[radii];%raddii of the circles
end

(三)correctImage函数

correctImage函数旨在校正图像的旋转和投影问题。它以原始图像(org_1.png)中四个圆形的质心作为参考点,通过fitgeotrans函数依据这些参考点调整图像方向,同时保持图像大小不变。针对投影或旋转图像中findCircles函数无法准确检测圆形的问题,采取了一系列手动干预步骤。示例代码如下:

matlab 复制代码
function correctedImage= correctImage(filenmae)
    %function that fix the projections/Rotations
    orgImage=LoadImage(filenmae);
    BandWimage=~im2bw(orgImage,0.5);%converting the image to black and white
    BandWfill=imfill(BandWimage,'holes');%filling any holes in the image
    medianFilt=medfilt2(BandWfill);%filtering using a median filter
    connectedComps=bwconncomp(medianFilt);%finding the connceted components in the image
    stats=regionprops(medianFilt,'Area','Centroid');%getting stats of the image(area,centroid)
    Areas=[stats.Area];
    Circles=zeros(connectedComps.ImageSize);%Isolating the circles
    for p = 1:connectedComps.NumObjects
        if stats(p).Area<max(Areas)
            Circles(connectedComps.PixelIdxList{p}) = 1;
        end
    end
    circlesLabeled=bwlabel(Circles, 8);%returning the label matrix L for 8 connceted objects (labeling the circles).
    circlesProps=regionprops(circlesLabeled,'Area','Centroid');%Stats of the circles
    circlesCentroids=[circlesProps.Centroid];%centroids of the circles
    varyingPoints=reshape(circlesCentroids,2,[]);%seting centroids of the image as reference points for transformation.
    MovingPointsT=transpose(varyingPoints);%transposing the matrix to get it in the correct form
    staticPoints=flip(findCircles(LoadImage('org_1.png')));%Setting the fixed points as centroids of org_1.png
    transformation=fitgeotrans(MovingPointsT,staticPoints,'Projective');%applying the transformation
    Reference=imref2d(size(LoadImage('org_1.png')));%referencing the size of org_1.png
    correctedImage=imwarp(orgImage,transformation,'OutputView',Reference);%determ ining pixel values for output image by mapping locations of the output to the input image
end

(四)findColours函数

findColours函数负责处理double类型图像,并生成代表16个方块颜色的字符串矩阵。由于部分图像存在噪声,为实现精确的颜色检测,进行了腐蚀、阈值处理、滤波和对比度调整等额外预处理操作。代码实现如下:

matlab 复制代码
function colours=findColours(image)
    %function to find the colours of the squares
    image = imerode(image,ones(5));%erroding the image
    image = medfilt3(image,[11 11 1]); % median filter to suppress noise
    image = imadjust(image,stretchlim(image,0.05)); % increase contrast of the image
    figure(),imshow(image)
    imageMask= rgb2gray(image)>0.08; %converting the image into greyscale
    imageMask = bwareaopen(imageMask,100); % removing positive specks
    imageMask = ~bwareaopen(~imageMask,100); % removing negative specks
    imageMask = imclearborder(imageMask); % removing outer white region
    imageMask = imerode(imageMask,ones(10)); % eroding to exclude edge effects
    % segmenting the image
    [L N] = bwlabel(imageMask);
    % getting average color in each image mask region
    maskColors = zeros(N,3);
    for p = 1:N % stepping throughout the patches
        imgmask = L==p;
        mask= image(imgmask(:,:,[1 1 1]));
        maskColors(p,:) = mean(reshape(mask,[],3),1);
    end
    % trying to snap the centers to a grid
    Stats = regionprops(imageMask,'centroid'); %obtaining the centroids
    Centroids = vertcat(Stats.Centroid);%concatenatoin
    centroidlimits = [min(Centroids,[],1); max(Centroids,[],1)];
    Centroids= round((Centroids-centroidlimits(1,:))./range(centroidlimits,1)*3 + 1);
    % reordering colour samples
    index = sub2ind([4 4],Centroids(:,2),Centroids(:,1));
    maskColors(index,:) = maskColors;
    % specify colour names and their references
    colorNames = {'white','red','green','blue','yellow'};
    colorReferences = [1 1 1; 1 0 0; 0 1 0; 0 0 1; 1 1 0];
    % finding color distances in RGB
    distance = maskColors - permute(colorReferences,[3 2 1]);
    distance = squeeze(sum(distance.^2,2));
    % finding index of closest match for each patch
    [~,index] = min(distance,[],2);
    % looking up colour names and returning a matrix of colour names
    colours = reshape(colorNames(index),4,4)
end

(五)colourMatrix函数

colourMatrix函数作为主集成函数,对数据集中的每一幅图像调用上述三个函数(LoadImagefindCirclesfindColours) 。在处理的30幅图像中,该函数成功识别了28幅图像的颜色,但在rot_2proj_1这两幅图像中,有3个方块颜色误分类,导致这两幅图像的错误率为18.75%(3/16个方块)。代码如下:

matlab 复制代码
%Function which calls other functions
%reading the Image and converting it to type double, then fixing the image
%for any projections or rotations, also finding the circles 
correctedImage=correctImage(filename);%fixing projections and rotations
figure(),imshow(correctedImage)
findCircles(correctedImage);%finding the circles
%Find The Colours of the Squares
colorRecognition=findColours(correctedImage);
finalImage=colorRecognition;

三、真实照片处理建议

项目中处理的"images_2"数据集是模拟图像,在查看真实照片后,为提升照片质量,提出以下建议:

  1. 固定拍摄角度:所有图像采用相同拍摄角度,避免因角度差异带来的图像变形问题。
  2. 统一光源:保证良好的光照条件,消除阴影影响,使图像色彩更真实、清晰。
  3. 更换优质相机:使用像素质量更高的相机,减少图像模糊,提高图像整体质量。
  4. 确保纸张平整:将图像放置在平坦表面,保证纸张笔直,防止图像出现扭曲。

四、项目总结

本项目运用MATLAB实现了识别图像中方块颜色的核心目标,且准确率较高。在处理的30幅图像中,28幅颜色识别准确率达100%,总体准确率为28/30 = 93.3%。在圆形检测以及校正旋转或投影图像方向方面,开发的函数成功率均为100%。通过此次项目,积累了丰富的MATLAB图像处理经验,同时也认识到在处理真实图像时还需进一步优化算法,以应对更复杂的图像场景。

需要代码可以联系

相关推荐
LingRannn15 分钟前
【最新Python包管理工具UV的介绍和安装】
开发语言·python·uv
Sheep Shaun1 小时前
C++类与对象—下:夯实面向对象编程的阶梯
c语言·开发语言·数据结构·c++·算法
AIGC魔法师2 小时前
轮播图导航组件 | 纯血鸿蒙组件库AUI
开发语言·harmonyos·openharmony·鸿蒙开发·纯血鸿蒙·arkui / ets·鸿蒙组件库aui
后藤十八里2 小时前
Python格式化字符串的四种方法
开发语言·python·学习
Little_Yuu3 小时前
抽奖系统(基于Tkinter)
开发语言·python
雨落俊泉3 小时前
2025-04-26-利用奇异值重构矩阵-美团
线性代数·矩阵·重构
福居路冥想的草莓3 小时前
矩阵置零(中等)
数据结构·算法·矩阵
sword devil9004 小时前
基于python生成taskc语言文件--时间片轮询
开发语言·python
dudly4 小时前
用Python打造自己的专属命令行工具
开发语言·python·batch命令
AI+程序员在路上4 小时前
Qt6.8中进行PDF文件读取和编辑
开发语言·qt·pdf