在MATLAB中选择颜色范围内的像素 [英] Selecting pixels within a range of color in MATLAB

查看:936
本文介绍了在MATLAB中选择颜色范围内的像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个项目,其中包括使用相机来检测电子元件是否被焊接。程序必须能够触发快照,当PLC请求它,分析它,并发送一个通过/失败标志回到plc。

I am working on a project that consists in using a camera to detect if an electronic component has been soldered or not. The program has to be able to trigger the snapshot when a PLC asks for it, analize it and send a pass/fail sign back to the plc.

一个MATLAB开始我一直在寻找信息,以了解它是否可行,并得到一个基本的想法从哪里开始。

As I'm a MATLAB begginer I have been searching for information to know if it's feasible and to get a basic idea of where to start.

我的想法是计算一个定义区域中有多少像素有银或金色调。如果它主要是黄金,这意味着它没有被焊接。

My idea would be to count how many pixels have a silver or gold tone in a defined zone. If it's mainly gold it means that it hasn't been soldered.

我的问题是,你将如何获得在一个

My question is, how would you do it to obtain the number of pixels that have a color inside a defined range inside a region of a webcam image?

我发现这是一个确切的颜色,而不是一个范围。

I have found this but it's for an exact color instead of a range.

count = sum(im(:,:1)== 255& im(:,:3)== 255& im(:,:3)== 255) p>

count = sum(im(:, :, 1) == 255 & im(:, :, 3) == 255 & im(:, :, 3) == 255);

推荐答案

最后,我使用了我发布但使用一个小的兴趣区域的功能。

In the end I used the function I posted but using a small region of interest.

我需要画一个正方形,围绕黄色区域,任何建议吗?

I need to draw a square arround the yellow zones, any suggestions?

所以有一些反馈(我不知道如何附加它,也许你不会去看到它)。评论在加泰罗尼亚语,但你不会难以理解我做了什么。

I'm attaching the code so there is some feedback (I'm not sure how to attach it, maybe you are not going to see it). The comments are in catalan but you won't have trouble understanding what I've done.

谢谢大家!

clear all
clc

info = imaqhwinfo('winvideo')           %Defineix origen de video
dev=info.DeviceInfo
vid=videoinput('winvideo',1)

vid.ROIPosition=[200 300 355 400];      %Zona a analitzar [iniciX, iniciY, ampladaX,
                                        alçadaY]
vid.FramesPerTrigger=5;                 %Millora la qualitat de la foto
src.Sharpness=5;

img=getsnapshot(vid);                   %Dispara foto


count = sum((img(:, :, 1) >= 150 & img(:, :, 1) <= 255) & (img(:, :, 2) >= 100 & img(:,
:, 2) <= 255) & (img(:, :, 3) >= 0 & img(:, :, 3) <= 100));
numP=sum(count(1,:))                    %Nombre de píxels en el rang de color donat
                                        %ARA DETECTA GROC/DAURAT

dimT=size(img(:,:,1));                  %Nombre de píxels total en la imatge
numT=dimT(1)*dimT(2)

Percentatge=numP/numT*100               %Percentatge de color en la imatge

%Hold on 
%Draw square
%imshow(img)
%Hold off

这篇关于在MATLAB中选择颜色范围内的像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆