当前位置: 首页 > news >正文

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

一、项目背景

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

二、方法与函数实现

(一)LoadImage函数

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

function [ image] = LoadImage( filename )%function to load the image and convert it to type doubleimage=imread(filename); %read the image fileif isa(image,'uint8')%check if the image is type unit8image=double(image)/255; %change the image type to doubleelseerror('The image is of unknown type');%return error message if image is not type unit8end
end

(二)findCircles函数

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

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 circlesradii=[radii];%raddii of the circles
end

(三)correctImage函数

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

function correctedImage= correctImage(filenmae)%function that fix the projections/RotationsorgImage=LoadImage(filenmae);BandWimage=~im2bw(orgImage,0.5);%converting the image to black and whiteBandWfill=imfill(BandWimage,'holes');%filling any holes in the imagemedianFilt=medfilt2(BandWfill);%filtering using a median filterconnectedComps=bwconncomp(medianFilt);%finding the connceted components in the imagestats=regionprops(medianFilt,'Area','Centroid');%getting stats of the image(area,centroid)Areas=[stats.Area];Circles=zeros(connectedComps.ImageSize);%Isolating the circlesfor p = 1:connectedComps.NumObjectsif stats(p).Area<max(Areas)Circles(connectedComps.PixelIdxList{p}) = 1;endendcirclesLabeled=bwlabel(Circles, 8);%returning the label matrix L for 8 connceted objects (labeling the circles).circlesProps=regionprops(circlesLabeled,'Area','Centroid');%Stats of the circlescirclesCentroids=[circlesProps.Centroid];%centroids of the circlesvaryingPoints=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 formstaticPoints=flip(findCircles(LoadImage('org_1.png')));%Setting the fixed points as centroids of org_1.pngtransformation=fitgeotrans(MovingPointsT,staticPoints,'Projective');%applying the transformationReference=imref2d(size(LoadImage('org_1.png')));%referencing the size of org_1.pngcorrectedImage=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个方块颜色的字符串矩阵。由于部分图像存在噪声,为实现精确的颜色检测,进行了腐蚀、阈值处理、滤波和对比度调整等额外预处理操作。代码实现如下:

function colours=findColours(image)%function to find the colours of the squaresimage = imerode(image,ones(5));%erroding the imageimage = medfilt3(image,[11 11 1]); % median filter to suppress noiseimage = imadjust(image,stretchlim(image,0.05)); % increase contrast of the imagefigure(),imshow(image)imageMask= rgb2gray(image)>0.08; %converting the image into greyscaleimageMask = bwareaopen(imageMask,100); % removing positive specksimageMask = ~bwareaopen(~imageMask,100); % removing negative specksimageMask = imclearborder(imageMask); % removing outer white regionimageMask = imerode(imageMask,ones(10)); % eroding to exclude edge effects% segmenting the image[L N] = bwlabel(imageMask);% getting average color in each image mask regionmaskColors = zeros(N,3);for p = 1:N % stepping throughout the patchesimgmask = L==p;mask= image(imgmask(:,:,[1 1 1]));maskColors(p,:) = mean(reshape(mask,[],3),1);end% trying to snap the centers to a gridStats = regionprops(imageMask,'centroid'); %obtaining the centroidsCentroids = vertcat(Stats.Centroid);%concatenatoincentroidlimits = [min(Centroids,[],1); max(Centroids,[],1)];Centroids= round((Centroids-centroidlimits(1,:))./range(centroidlimits,1)*3 + 1);% reordering colour samplesindex = sub2ind([4 4],Centroids(:,2),Centroids(:,1));maskColors(index,:) = maskColors;% specify colour names and their referencescolorNames = {'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 RGBdistance = 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 namescolours = reshape(colorNames(index),4,4)
end

(五)colourMatrix函数

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

%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图像处理经验,同时也认识到在处理真实图像时还需进一步优化算法,以应对更复杂的图像场景。

需要代码可以联系
在这里插入图片描述

http://www.xdnf.cn/news/288613.html

相关文章:

  • 大模型推理--从零搭建大模型推理服务器:硬件选购、Ubuntu双系统安装与环境配置
  • Python实战:基于控制台与MySQL的电影票预订系统开发指南
  • 学习路线(机器人系统)
  • 模糊控制理论(含仿真)
  • 7400MB/s5050TBW完美结合,全新希捷酷玩530R SSD体验评测
  • 10 种最新的思维链(Chain-of-Thought, CoT)增强方法
  • 攻防世界-php伪协议和文件包含
  • 第一章-Rust入门
  • 音频感知动画新纪元:Sonic让你的作品更生动
  • PE文件结构(导出表)
  • 专家系统的推理流程深度解析
  • Java SE(8)——继承
  • 虚拟dom是什么,他有什么好处
  • 深度学习里程碑:AlexNet 架构解析与核心技术详解
  • 【深度学习|学习笔记】Deep Belief Network(DBN,深度置信网络)起源、原理、发展和应用(附代码)
  • 【KWDB 创作者计划】基于 ESP32 + KWDB 的智能环境监测系统实战
  • 高可用架构设计——故障响应
  • Red Hat6.4环境下搭建DHCP服务器
  • 第六章 流量特征分析-蚁剑流量分析(玄机靶场系列)
  • MCP原理详解及实战案例(动嘴出UI稿、3D建模)
  • Linux系统安装PaddleDetection
  • 基于CBOW模型的词向量训练实战:从原理到PyTorch实现
  • 使用AI 将文本转成视频 工具 介绍
  • 实验-数字电路设计2-复用器和七段数码管(数字逻辑)
  • 在Ubuntu系统中安装桌面环境
  • 路由器详细讲解
  • Docker —— 隔离的基本操作(1)
  • SpringCloud GateWay网关
  • 排序用法(Arrays.sort)
  • AI笔记-1