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 "ObjectFinder.h"
00049
00050 #include "ObjectColorSegmenter.h"
00051 #include "Image/ImageProcessor.h"
00052 #include "Image/PrimitivesDrawer.h"
00053 #include "Image/ByteImage.h"
00054 #include "Interfaces/RegionFilterInterface.h"
00055 #include "Interfaces/ObjectClassifierInterface.h"
00056 #include "Helpers/helpers.h"
00057
00058 #include <stdio.h>
00059 #include <math.h>
00060
00061
00062
00063
00064
00065
00066
00067 CObjectFinder::CObjectFinder()
00068 {
00069 m_pObjectColorSegmenter = new CObjectColorSegmenter();
00070
00071 m_pSegmentedImage = 0;
00072 m_pRegionFilter = 0;
00073 m_nIDCounter = 0;
00074 m_bUseROI = false;
00075 }
00076
00077 CObjectFinder::~CObjectFinder()
00078 {
00079 delete m_pObjectColorSegmenter;
00080
00081 if (m_pSegmentedImage)
00082 delete m_pSegmentedImage;
00083 }
00084
00085
00086
00087
00088
00089
00090 void CObjectFinder::SetColorParameterSet(const CColorParameterSet *pColorParameterSet)
00091 {
00092 m_pObjectColorSegmenter->SetColorParameterSet(pColorParameterSet);
00093 }
00094
00095 void CObjectFinder::PrepareImages(const CByteImage *pImage, float fROIFactor, bool bCalculateHSVImage)
00096 {
00097 if (m_pSegmentedImage && (m_pSegmentedImage->width != pImage->width || m_pSegmentedImage->height != pImage->height))
00098 {
00099 delete m_pSegmentedImage;
00100 m_pSegmentedImage = 0;
00101 }
00102
00103 if (!m_pSegmentedImage)
00104 m_pSegmentedImage = new CByteImage(pImage->width, pImage->height, CByteImage::eGrayScale);
00105
00106 m_bUseROI = fROIFactor != -1;
00107 m_ROIList.clear();
00108
00109 if (m_bUseROI)
00110 {
00111 for (int i = 0; i < (int) m_objectList.size(); i++)
00112 {
00113
00114 Object2DEntry entry = m_objectList.at(i);
00115
00116 const float w = floor((entry.region.max_x - entry.region.min_x + 1) * fROIFactor * 0.5f);
00117 const float h = floor((entry.region.max_y - entry.region.min_y + 1) * fROIFactor * 0.5f);
00118
00119 entry.region.min_x = int(entry.region.centroid.x - w - 0.5f);
00120 entry.region.min_y = int(entry.region.centroid.y - h - 0.5f);
00121 entry.region.max_x = int(entry.region.centroid.x + w + 0.5f);
00122 entry.region.max_y = int(entry.region.centroid.y + h + 0.5f);
00123
00124 m_ROIList.push_back(entry);
00125 }
00126 }
00127
00128 if (bCalculateHSVImage)
00129 m_pObjectColorSegmenter->SetImage(pImage, &m_ROIList);
00130
00131 m_objectList.clear();
00132 }
00133
00134 void CObjectFinder::FindObjects(const CByteImage *pImage, CByteImage *pResultImage, ObjectColor color, int nMinPointsPerRegion, bool bShowSegmentedImage)
00135 {
00136 RegionList regionList;
00137
00138
00139 switch (color)
00140 {
00141 case eColored:
00142 m_pObjectColorSegmenter->FindColoredRegions(m_pSegmentedImage, regionList, nMinPointsPerRegion);
00143
00144 default:
00145 m_pObjectColorSegmenter->FindRegionsOfGivenColor(m_pSegmentedImage, color, regionList, nMinPointsPerRegion);
00146 }
00147
00148 if (bShowSegmentedImage && pResultImage)
00149 ImageProcessor::ConvertImage(m_pSegmentedImage, pResultImage);
00150
00151
00152 CheckRegionsForObjects(pImage, m_pSegmentedImage, pResultImage, regionList, color);
00153 }
00154
00155 void CObjectFinder::FindObjects(const CByteImage *pImage, CByteImage *pResultImage, ObjectColor color, int nMinPointsPerRegion, CByteImage *pResultSegmentedImage)
00156 {
00157 RegionList regionList;
00158
00159
00160 switch (color)
00161 {
00162 case eColored:
00163 m_pObjectColorSegmenter->FindColoredRegions(pResultSegmentedImage, regionList, nMinPointsPerRegion);
00164
00165 default:
00166 m_pObjectColorSegmenter->FindRegionsOfGivenColor(pResultSegmentedImage, color, regionList, nMinPointsPerRegion);
00167 }
00168
00169
00170 CheckRegionsForObjects(pImage, pResultSegmentedImage, pResultImage, regionList, color);
00171 }
00172
00173 void CObjectFinder::FindObjects(const CByteImage *pImage, CByteImage *pResultImage, ObjectColor color, int hue, int hue_tol, int min_sat, int max_sat, int min_v, int max_v, int nMinPointsPerRegion, bool bShowSegmentedImage)
00174 {
00175 RegionList regionList;
00176
00177
00178 m_pObjectColorSegmenter->FindRegionsOfGivenColor(m_pSegmentedImage, color, hue, hue_tol, min_sat, max_sat, min_v, max_v, regionList, nMinPointsPerRegion);
00179
00180 if (bShowSegmentedImage)
00181 ImageProcessor::ConvertImage(m_pSegmentedImage, pResultImage);
00182
00183
00184 CheckRegionsForObjects(pImage, m_pSegmentedImage, pResultImage, regionList, color);
00185 }
00186
00187 void CObjectFinder::FindObjectsInSegmentedImage(const CByteImage *pSegmentedImage, CByteImage *pResultImage, ObjectColor color, int nMinPointsPerRegion, bool bShowSegmentedImage)
00188 {
00189 RegionList regionList;
00190
00191
00192 ImageProcessor::FindRegions(pSegmentedImage, regionList, nMinPointsPerRegion, 0, true, true);
00193
00194 if (bShowSegmentedImage)
00195 ImageProcessor::ConvertImage(pSegmentedImage, pResultImage);
00196
00197
00198 CheckRegionsForObjects(0, pSegmentedImage, pResultImage, regionList, color);
00199 }
00200
00201 void CObjectFinder::CheckRegionsForObjects(const CByteImage *pColorImage, const CByteImage *pSegmentedImage, CByteImage *pResultImage, RegionList ®ionList, ObjectColor color)
00202 {
00203 const int nRegions = (int) regionList.size();
00204
00205 for (int i = 0; i < nRegions; i++)
00206 {
00207 const MyRegion ®ion = regionList.at(i);
00208
00209 if (!m_pRegionFilter || m_pRegionFilter->CheckRegion(pColorImage, pSegmentedImage, region))
00210 {
00211 Object2DEntry objectEntry;
00212 objectEntry.color = color;
00213 objectEntry.region = region;
00214 objectEntry.type = eCompactObject;
00215 objectEntry.sName = "CompactObject";
00216
00217 float min = 100.0f;
00218 int best_id = -1;
00219
00220 for (int j = 0; j < (int) m_ROIList.size(); j++)
00221 {
00222 const Object2DEntry &entry = m_ROIList.at(j);
00223 const float fRatio = entry.region.nPixels < region.nPixels ? float(entry.region.nPixels) / region.nPixels : float(region.nPixels) / entry.region.nPixels;
00224 const float fDistance = Math2d::Distance(entry.region.centroid, region.centroid);
00225
00226 if (entry.color == color && fRatio > 0.75f && fDistance < min)
00227 {
00228 min = fDistance;
00229 best_id = entry.id;
00230 }
00231 }
00232
00233 if (best_id == -1)
00234 objectEntry.id = m_nIDCounter++;
00235 else
00236 objectEntry.id = best_id;
00237
00238 m_objectList.push_back(objectEntry);
00239
00240 if (pResultImage)
00241 PrimitivesDrawer::DrawRegion(pResultImage, region, 255, 0, 0, 2);
00242 }
00243 }
00244 }
00245
00246 int CObjectFinder::Finalize()
00247 {
00248 return (int) m_objectList.size();
00249 }
00250
00251 void CObjectFinder::ClearObjectList()
00252 {
00253 m_objectList.clear();
00254 }
00255
00256 void CObjectFinder::AddObject(const Object2DEntry &entry)
00257 {
00258 m_objectList.push_back(entry);
00259 }