00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 #include <new>
00047
00048 #include "ObjectColorSegmenter.h"
00049
00050 #include "Image/ImageProcessor.h"
00051 #include "Image/ByteImage.h"
00052 #include "Color/ColorParameterSet.h"
00053
00054
00055
00056
00057
00058
00059
00060 CObjectColorSegmenter::CObjectColorSegmenter()
00061 {
00062 m_pColorParameterSet = 0;
00063
00064 m_pTempImage = 0;
00065 m_pHSVImage = 0;
00066 m_pRGBImage = 0;
00067
00068 m_pROIList = 0;
00069 }
00070
00071 CObjectColorSegmenter::~CObjectColorSegmenter()
00072 {
00073 if (m_pHSVImage)
00074 delete m_pHSVImage;
00075
00076 if (m_pTempImage)
00077 delete m_pTempImage;
00078 }
00079
00080
00081
00082
00083
00084
00085 void CObjectColorSegmenter::SetImage(const CByteImage *pImage, const Object2DList *pROIList)
00086 {
00087 m_pRGBImage = pImage;
00088
00089 if (m_pTempImage && (m_pTempImage->width != pImage->width || m_pTempImage->height != pImage->height))
00090 {
00091 delete m_pTempImage;
00092 m_pTempImage = 0;
00093 }
00094
00095 if (!m_pTempImage)
00096 m_pTempImage = new CByteImage(pImage->width, pImage->height, CByteImage::eGrayScale);
00097
00098 if (m_pHSVImage && !m_pHSVImage->IsCompatible(pImage))
00099 {
00100 delete m_pHSVImage;
00101 m_pHSVImage = 0;
00102 }
00103
00104 if (!m_pHSVImage)
00105 m_pHSVImage = new CByteImage(pImage);
00106
00107 m_pROIList = pROIList;
00108
00109 if (m_pROIList && m_pROIList->size() != 0)
00110 {
00111 const int nSize = (int) m_pROIList->size();
00112 for (int i = 0; i < nSize; i++)
00113 ImageProcessor::CalculateHSVImage(pImage, m_pHSVImage, &m_pROIList->at(i).region);
00114 }
00115 else
00116 ImageProcessor::CalculateHSVImage(pImage, m_pHSVImage);
00117 }
00118
00119 void CObjectColorSegmenter::SetColorParameterSet(const CColorParameterSet *pColorParameterSet)
00120 {
00121 m_pColorParameterSet = pColorParameterSet;
00122 }
00123
00124 void CObjectColorSegmenter::FindColoredRegions(CByteImage *pResultImage, RegionList ®ionList, int nMinPointsPerRegion)
00125 {
00126 if (!m_pHSVImage)
00127 {
00128 printf("error: call CObjectColorSegmenter::SetImage first and do not use optimizer\n");
00129 return;
00130 }
00131
00132 ImageProcessor::CalculateSaturationImage(m_pHSVImage, pResultImage);
00133 ImageProcessor::ThresholdBinarize(pResultImage, pResultImage, 120);
00134
00135 ImageProcessor::Erode(pResultImage, m_pTempImage);
00136 ImageProcessor::Dilate(m_pTempImage, pResultImage);
00137
00138 ImageProcessor::FindRegions(pResultImage, regionList, nMinPointsPerRegion, 0, true, true);
00139 }
00140
00141 void CObjectColorSegmenter::CalculateSegmentedImage(CByteImage *pResultImage, ObjectColor color)
00142 {
00143 if (!m_pColorParameterSet)
00144 {
00145 printf("error: color parameter set has not been set\n");
00146 return;
00147 }
00148
00149 if (!m_pHSVImage)
00150 {
00151 printf("error: call CObjectColorSegmenter::SetImage first\n");
00152 return;
00153 }
00154
00155 const int *pParameters = m_pColorParameterSet->GetColorParameters(color);
00156
00157 if (!pParameters)
00158 {
00159 printf("error: could not load parameters for segmenting color\n");
00160 return;
00161 }
00162
00163 if (m_pROIList && m_pROIList->size() != 0)
00164 {
00165 ImageProcessor::Zero(pResultImage);
00166
00167 const int nSize = (int) m_pROIList->size();
00168 for (int i = 0; i < nSize; i++)
00169 {
00170 const Object2DEntry &entry = m_pROIList->at(i);
00171
00172 if (entry.color == color)
00173 ImageProcessor::FilterHSV(m_pHSVImage, pResultImage, pParameters[0], pParameters[1], pParameters[2], pParameters[3], pParameters[4], pParameters[5], &entry.region);
00174 }
00175 }
00176 else
00177 {
00178 ImageProcessor::FilterHSV(m_pHSVImage, pResultImage, pParameters[0], pParameters[1], pParameters[2], pParameters[3], pParameters[4], pParameters[5]);
00179 }
00180 }
00181
00182
00183 void CObjectColorSegmenter::FindRegionsOfGivenColor(CByteImage *pResultImage, ObjectColor color, RegionList ®ionList, int nMinPointsPerRegion)
00184 {
00185 CalculateSegmentedImage(pResultImage, color);
00186
00187 if (m_pROIList && m_pROIList->size() != 0)
00188 {
00189 const int nSize = (int) m_pROIList->size();
00190 for (int i = 0; i < nSize; i++)
00191 {
00192 const Object2DEntry &entry = m_pROIList->at(i);
00193
00194 if (entry.color == color)
00195 {
00196 ImageProcessor::Erode(pResultImage, m_pTempImage, 3, &entry.region);
00197 ImageProcessor::Dilate(m_pTempImage, pResultImage, 3, &entry.region);
00198 }
00199 }
00200 }
00201 else
00202 {
00203 ImageProcessor::Erode(pResultImage, m_pTempImage);
00204 ImageProcessor::Dilate(m_pTempImage, pResultImage);
00205 }
00206
00207 ImageProcessor::FindRegions(pResultImage, regionList, nMinPointsPerRegion, 0, true, true);
00208 }
00209
00210 void CObjectColorSegmenter::FindRegionsOfGivenColor(CByteImage *pResultImage, ObjectColor color, int hue, int hue_tol, int min_sat, int max_sat, int min_v, int max_v, RegionList ®ionList, int nMinPointsPerRegion)
00211 {
00212 if (!m_pHSVImage)
00213 {
00214 printf("error: call CObjectColorSegmenter::SetImage first\n");
00215 return;
00216 }
00217
00218 if (m_pROIList && m_pROIList->size() != 0)
00219 {
00220 ImageProcessor::Zero(pResultImage);
00221
00222 const int nSize = (int) m_pROIList->size();
00223 for (int i = 0; i < nSize; i++)
00224 {
00225 const Object2DEntry &entry = m_pROIList->at(i);
00226
00227 if (entry.color == color)
00228 {
00229 ImageProcessor::FilterHSV(m_pHSVImage, pResultImage, hue, hue_tol, min_sat, max_sat, min_v, max_v, &entry.region);
00230 ImageProcessor::Erode(pResultImage, m_pTempImage, 3, &entry.region);
00231 ImageProcessor::Dilate(m_pTempImage, pResultImage, 3, &entry.region);
00232 }
00233 }
00234 }
00235 else
00236 {
00237 ImageProcessor::FilterHSV(m_pHSVImage, pResultImage, hue, hue_tol, min_sat, max_sat, min_v, max_v);
00238 ImageProcessor::Erode(pResultImage, m_pTempImage);
00239 ImageProcessor::Dilate(m_pTempImage, pResultImage);
00240 }
00241
00242
00243 ImageProcessor::FindRegions(pResultImage, regionList, nMinPointsPerRegion, 0, true, true);
00244 }