要每天养成写博客的习惯,可是,苦于最近学习没有进展啊。
无奈把自己去年学习STM32以及做相关项目时的代码发出来水水博客吧!
本代码在原子哥摄像头实验基础上做了相关更改。
color.c部分[用于设定颜色值、匹配颜色等]:
#include "color.h"
#include "sys.h"
#include "lcd.h"
#include <stdio.h>
RESULT result;
TARGET_CONDITION condition={
60, //目标最小色度,H_MIN
110, //目标最大色度,H_MAX
50, //目标最小饱和度,S_MIN
240, //目标最大饱和度,S_MAX
60, //目标最小亮度,L_MIN
190, //目标最大亮度,L_MAX
40, //目标最小宽度,WIDTH_MIN
40, //目标最小高度,HEIGHT_MIN
320, //目标最大宽度,WIDTH_MAX
240 //目标最大高度,HEIGHT_MAX蟾叨龋琀EIGHT_MAX
};
typedef struct //HLS颜色
{
unsigned char Hue; //色度 ,[0,240]
unsigned char Lightness; //亮度,[0,240]
unsigned char Saturation; //饱和度,[0,240]
}COLOR_HLS;
typedef struct //RGB
{
unsigned char Red; // [0,255]
unsigned char Green; // [0,255]
unsigned char Blue; // [0,255]
}COLOR_RGB;
/**************************************/
//读取某点的颜色
static void ReadColor( uint16_t usX, uint16_t usY, COLOR_RGB* color_rgb )
{
unsigned short rgb;
rgb = LCD_ReadPoint( usX, usY ); //获取颜色数据
//转换成值域为[0,255]的三原色值
color_rgb->Red = (unsigned char)( ( rgb & 0xF800 ) >> 8 );
color_rgb->Green = (unsigned char)( ( rgb & 0x07E0 ) >> 3 );
color_rgb->Blue = (unsigned char)( ( rgb & 0x001F ) << 3 );
}
/*************************************/
//RGB转换为HLS
//H:色度
//L:亮度
//S:饱和度
static void RGB2HSL( const COLOR_RGB* color_rgb, COLOR_HLS* color_hls )
{
int r, g, b;
int h, l, s;
int max, min, dif;
r = color_rgb->Red;
g = color_rgb->Green;
b = color_rgb->Blue;
max = maxOf3Values( r, g, b );
min = minOf3Values( r, g, b );
dif = max - min;
//计算l,亮度
l = ( max + min ) * 240 / 255 / 2;
//计算h,色度
if( max == min )//无定义
{
s = 0;
h = 0;
}
else
{
//计算色度
if( max == r )
{
if( min == b )//h介于0到40
{
h = 40 * ( g - b ) / dif;
}
else if( min == g )//h介于200到240
{
h = 40 * ( g - b ) / dif + 240;
}
}
else if( max == g )
{
h = 40 * ( b - r ) / dif + 80;
}
else if( max == b )
{
h = 40 * ( r - g ) / dif + 160;
}
//计算饱和度
if( l == 0 )
{
s = 0;
}
else if( l <= 120 )
{
s = dif * 240 / ( max + min );
}
else
{
s = dif * 240 / ( 480 - ( max + min ) );
}
}
color_hls->Hue = h; //色度
color_hls->Lightness = l; //亮度
color_hls->Saturation = s; //饱和度
}
/************************************************/
// 颜色匹配
//color_hls :COLOR_HLS结构体,存储HLS格式颜色数据
// condition :TARGET_CONDITION结构体,存放希望的颜色数据阈值
// 1:像素点颜色在目标范围内;0:像素点颜色不在目标范围内。
static