bool CapsuleProc::RectangleBlob ( IMG image, const TRect2D<long>& roi, long wndSize, long dynThres, long minBlobSize, TBlobTouchBorderFilter ignorBorder, long minBlobWidth, long minBlobHeight, bool alsoReverse) { if(!IsImage(image) || roi.IsNull()) { return true; } IMG imageROI = NULL; CreateImageMap(image , roi.x0(), roi.y0(), roi.x1(), roi.y1(), roi.Width(), roi.Height(), imageROI); bool success = RectBlackBlob(imageROI, wndSize, dynThres, minBlobSize, ignorBorder, minBlobWidth, minBlobHeight); if(alsoReverse && success) { IMG imageROI2 = NULL; long maskVal[] = { 255 }; XorConstant(imageROI, maskVal, imageROI2); success = RectBlackBlob(imageROI2, wndSize, dynThres, minBlobSize, ignorBorder, minBlobWidth, minBlobHeight); ReleaseImage(imageROI2); } m_sortObserver.ObserverIMG(SortObserver::MPart, imageROI); m_sortObserver.ObserverIMG(SortObserver::CPart, imageROI); ReleaseImage(imageROI); return success; }
/*************************************************************** *函数名称: TransSobelAnalysis * * 参数: IMG 图像 * * * 返回值: 不存在斑点 true, * 存在斑点 false * * 功能: 在指定的区域内采用sobel滤波+ * 斑点分析的方法检测是否存在斑点 * ****************************************************************/ bool CapsuleProc::TransSobelAnalysis(IMG image) { IMG copyImage = 0; CreateDuplicateImage (image, copyImage); HorizonExtend::Extend(copyImage); long width = ImageWidth(copyImage); long height= ImageHeight(copyImage); TRect2D<long> roi = TRect2D<long>(TPoint2D<long>(0,0), width, height); roi.Expand(-2, -2); bool success = false; IMG imageROI= NULL; CreateImageMap( copyImage, roi.x0(), roi.y0(), roi.x1(), roi.y1(), roi.Width(), roi.Height(), imageROI); if(IsImage(imageROI)) { IMG imageSobel = 0; FilterSobelVertical(imageROI, FM_5x5, imageSobel); success = RectBlackBlob ( imageSobel, m_segmentParam.dynWndSize, m_segmentParam.dynThres - 2, m_segmentParam.blobSize, FBLOB_BORDER_ALL, -1, 4); m_sortObserver.ObserverIMG(SortObserver::MSobel, copyImage); m_sortObserver.ObserverIMG(SortObserver::CSobel, imageSobel); ReleaseImage(imageSobel); } ReleaseImage(imageROI); ReleaseImage(copyImage); return success; }
CapsuleProc::~CapsuleProc() { ReleaseImage(m_cvbImg); ReleaseImage(m_cvbProfileImg); ReleaseImage(m_profileBlob); ReleaseImage(m_cvbBlob); LMDestroy(m_lightMeter); }
/*************************************************************** *函数名称: ReInit * * 参数: TImgDim 图像尺寸信息 * * 返回值: 成功 true, 失败 false * * 功能: 初始化图像处理函数 ****************************************************************/ bool CapsuleProc::ReInit (const TImgDim& imgDimension) { if(m_dim == imgDimension) return true; m_dim = imgDimension; ReleaseImage(m_cvbImg); ReleaseImage(m_cvbProfileImg); ReleaseImage(m_profileBlob); ReleaseImage(m_cvbBlob); LMDestroy(m_lightMeter); m_imgBuffer.ReInit(m_dim, 30); m_rawImage = TAlloc<PixelMem>(m_dim.bytesPerRow * m_dim.height, e16ByteAlign); long pitchPlane = (m_dim.bytesPerPixel == 1) ? 0 : 1; if(0 != CreateImageFromPointer( m_rawImage.Base(), m_rawImage.Size(), m_dim.width, m_dim.height, m_dim.bytesPerPixel, 8, m_dim.bytesPerPixel, m_dim.bytesPerRow, pitchPlane, 0, 0, 0, m_cvbImg)) { return false; } TRect2D<int> boundary(TPoint2D<int>(0, 0), m_dim.width, m_dim.height); m_profileImage = TImage<PelGray8> (boundary, e16ByteAlign); if(0 != CreateImageFromPointer( m_profileImage.Base(), m_profileImage.RowSpace() * m_profileImage.Height(), m_profileImage.Width(), m_profileImage.Height(), 1, 8, 1, m_profileImage.RowSpace(), 0, 0, 0, 0, m_cvbProfileImg)) { return false; } m_cvbBlob = FBlobCreate(m_cvbImg, 0); m_profileBlob = FBlobCreate(m_cvbProfileImg, 0); if(0 != LMCreate(&m_lightMeter)) { return false; } return true; }
/*************************************************************** *函数名称: RectSobelBlob * * 参数: IMG 图像 * TRect2D 感兴趣区域 * long 窗口面积 * long 动态阈值 * long 斑点的最小面积 * TBlobTouchBorderFilter 忽略边界 * long 斑点最小宽度 * long 斑点最小高度 * 返回值: 不存在斑点 true, * 存在斑点 false * * 功能: 在指定的区域内采用动态阈值+ * 斑点分析的方法检测是否存在斑点 * ****************************************************************/ bool CapsuleProc::RectSobelBlob ( IMG image, const TRect2D<long>& roi, long wndSize, long dynThres, long minBlobSize, TBlobTouchBorderFilter ignorBorder, long minBlobWidth, long minBlobHeight) { if(!IsImage(image) || roi.IsNull() ) { return true; } bool success = true; IMG imageROI= NULL; CreateImageMap(image , roi.x0(), roi.y0(), roi.x1(), roi.y1(), roi.Width(), roi.Height(), imageROI); if(IsImage(imageROI)) { IMG imageSobel = 0; FilterSobelVertical(imageROI, FM_5x5, imageSobel); success = RectBlackBlob( imageSobel, wndSize, dynThres, minBlobSize, ignorBorder, minBlobWidth, minBlobHeight); if(success) { IMG imageSobel2 = NULL; long maskVal[] = { 255 }; XorConstant ( imageSobel, maskVal, imageSobel2); success = RectBlackBlob ( imageSobel2, wndSize, dynThres, minBlobSize, ignorBorder, minBlobWidth, minBlobHeight); ReleaseImage(imageSobel2); } //m_sortObserver.ObserverIMG(SortObserver::MSobel, image); //m_sortObserver.ObserverIMG(SortObserver::CSobel, imageSobel); ReleaseImage(imageSobel); } ReleaseImage(imageROI); return success; }
void ImProc::SetRefImage() { //release the previous images (if any) ReleaseImage(&m_ReferenceGrey); ReleaseImage(&m_Reference); IplImage *Frame=cvQueryFrame(m_Video); // get a frame of video //now copy a permanant copy of it into the m_Reference frame if(Frame !=0) { m_Reference = cvCloneImage(Frame); //and make a greyscale copy of it m_ReferenceGrey = ConvertToGrey(m_Reference); } }
inline void WImageBufferC<T, C>::Allocate(int width, int height) { if (IsNull() || WImage<T>::Width() != width || WImage<T>::Height() != height) { ReleaseImage(); WImageC<T, C>::SetIpl(cvCreateImage(cvSize(width, height),WImage<T>::Depth(), C)); } }
int main() { IplImage *img = cvLoadImage("target.jpg"); SetImageFloodFill(img); ShowImage(img); ReleaseImage(img); return 0; }
inline void WImageBuffer<T>::Allocate(int width, int height, int nchannels) { if (IsNull() || WImage<T>::Width() != width || WImage<T>::Height() != height || WImage<T>::Channels() != nchannels) { ReleaseImage(); WImage<T>::image_ = cvCreateImage(cvSize(width, height), WImage<T>::Depth(), nchannels); } }
bool CSoftWareConfig::LoadMutiFrameImage(LPCSTR filePath) { bool success = false; TMfdArchive mfdFile; if(mfdFile.Load(filePath)) { m_imgBuff = mfdFile.GetImgBuffer(); if(!m_imgBuff.IsNull()) { TImgDim dim = m_imgBuff.Dimension(); if(3==dim.bytesPerPixel) { m_hue1Vect.clear(); m_hue2Vect.clear(); } else { m_dimVect.clear(); } if(!(m_imgPlayBuff.Dimension() == dim)) { m_imgPlayBuff.ReInit(dim); if(m_IMGForPlayer) ReleaseImage(m_IMGForPlayer); long pitchPlane = (dim.bytesPerPixel == 1) ? 0 : 1; CreateImageFromPointer( m_imgPlayBuff.FrameData(0), m_imgPlayBuff.FrameBytes(), dim.width, dim.height, dim.bytesPerPixel, 8, dim.bytesPerPixel, dim.bytesPerRow, pitchPlane, 0, 0, 0, m_IMGForPlayer); } std::string fileName = filePath; if (fileName.find("Mono") <= fileName.size()) { m_pProcessor = ((CapsuleSorterDlg*)AfxGetMainWnd())->GetProcessor(1); } else{ m_pProcessor = ((CapsuleSorterDlg*)AfxGetMainWnd())->GetProcessor(3); } m_radiusScroll.SetScrollPos(m_pProcessor->RemainInfo().radius ); m_radius = m_pProcessor->RemainInfo().radius; m_minboxWidth = m_pProcessor->RemainInfo().minBoxWidth; m_imgBuff.GoNextFrame(); DisplayImage(); Process(m_imgBuff); success = true; UpdateData(FALSE); } } return success; }
void TgaLoad_Exit() { while(Bucket.ImageCount) { if(Bucket.ImageList[Bucket.ImageCount-1].uTexID) { glDeleteTextures(1,&Bucket.ImageList[Bucket.ImageCount-1].uTexID); } ReleaseImage(Bucket.ImageList[Bucket.ImageCount-1].pImage); } }
void ReleaseTexture(u32 uTexID) { i32 i; for(i=0;(u32)i<Bucket.ImageCount;i++) { if(Bucket.ImageList[i].uTexID == uTexID) { ReleaseImage(Bucket.ImageList[i].pImage); break; } } }
bool CapsuleProc::RotateIMG( size_t blobIndex, FBLOB profileBlob, IMG proImg, IMG &proSubIMG, size_t &posIndex) { long profileCount = 0; FBlobGetNumBlobs (profileBlob, profileCount); if ( blobIndex >= static_cast<size_t>(profileCount) ) { posIndex = -1; return false; } //Get Range long x0 = 0, y0 = 0, width = 0, height = 0; FBlobGetBoundingBox (profileBlob, blobIndex, x0, y0, width, height); posIndex = CheckPosition(x0+width/2, y0+height/2); IMG proIMGMap = NULL; CreateImageMap (proImg, x0, y0, x0+width, y0+height, width, height, proIMGMap); double rabi = 0.0, rabi1 = 0.0, rabi2 = 0.0, momentAngle = 0.0; FBlobGetMoments (profileBlob, blobIndex, rabi, rabi1, rabi2, momentAngle); static const double ADIVPI = 180.0 /(4.0*atan(1.0)); //ppi = 180/PI static const double ANGLE90 = 90; //ppiH = 180/PI*PI/2 double angle = 0.0; if(momentAngle>=0) { angle = (ANGLE90 - ADIVPI * momentAngle); } else { angle = -(ANGLE90 + ADIVPI * momentAngle); } RotateImage( proIMGMap, angle, IP_Linear, proSubIMG); TCoordinateMap cm; InitCoordinateMap(cm); SetImageCoordinates (proSubIMG, cm); ShrinkVertical (proSubIMG, m_segmentParam.shrinkY); ShrinkHorizontal(proSubIMG, m_segmentParam.shrinkX); ReleaseImage (proIMGMap); return true; }
bool CapsuleProc::SobelAnalysis(IMG image) { IMG copyImage = 0; CreateDuplicateImage (image, copyImage); HorizonExtend::Extend(copyImage); long width = ImageWidth(copyImage); long height= ImageHeight(copyImage); TRect2D<long> roi = TRect2D<long>(TPoint2D<long>(0,0), width, height); roi.Expand(-2, -2); bool success = RectSobelBlob( copyImage, roi, m_segmentParam.dynWndSize, m_segmentParam.dynThres, m_segmentParam.blobSize, FBLOB_BORDER_ALL); ReleaseImage(copyImage); return success; }
bool CapsuleProc::RectBlackBlob( IMG image, long wndSize, long dynThres, long minBlobSize, TBlobTouchBorderFilter ignorBorder, long minBlobWidth, long minBlobHeight) { if(!IsImage(image)) { return true; } IMG DynamicImage = NULL; CreateDynamicThresholdImage(image, wndSize, dynThres, DynamicImage); FBlobSetImage (m_cvbBlob, DynamicImage, 0); FBlobSetObjectFeatureRange (m_cvbBlob, 0, 128); FBlobSetSkipBinarization (m_cvbBlob, FALSE); FBlobSetLimitArea (m_cvbBlob, minBlobSize, -1); FBlobSetLimitWidth (m_cvbBlob, minBlobWidth, -1); FBlobSetLimitHeight (m_cvbBlob, minBlobHeight, -1); FBlobSetObjectTouchBorder (m_cvbBlob, ignorBorder); FBlobExec (m_cvbBlob); long blobCount = 0; FBlobGetNumBlobs(m_cvbBlob, blobCount); if(1 == m_dim.bytesPerPixel) { m_sortObserver.ObserverIMG(SortObserver::MDyn, DynamicImage); } else { m_sortObserver.ObserverIMG(SortObserver::CDyn, DynamicImage); } ReleaseImage(DynamicImage); return (blobCount == 0); }
bool SortObserver::ObserverIMG(ETarget eTarget, IMG img) { bool success = false; bool isImage = true; size_t target = 0; switch(eTarget) { case MAll: target = MAll; break; case MBin: target = MBin; break; case MEnh: target = MEnh; break; case MAnd: target = MAnd; break; case CAll: target = CAll; break; case MSub: target = m_mSub++; break; case MPart: target = m_mPart++; break; case MDyn: target = m_mDyn++; break; case MSobel: target = m_mSoble++; break; case CSub: target = m_cSub++; break; case CPart: target = m_cPart++; break; case CDyn: target = m_cDyn++; break; case CSobel: target = m_cSoble++; break; default: isImage = false; } if(isImage && (m_specTarget==target)) { ReleaseImage(m_targetIMG); CreateDuplicateImage(img, m_targetIMG); //m_targetIMG = img; //ShareImage(m_targetIMG); success = true; } return success; }
SortObserver::~SortObserver() { ReleaseImage(m_targetIMG); }
// Set the data to point to an image, releasing the old data void SetIpl(IplImage* img) { ReleaseImage(); WImageC<T, C>::SetIpl(img); }
void ImProc::UpdateFrame() { if(!m_Video) return; //release the previous frame ReleaseImage(&m_PrevFrame); //make the previous Frame this frame if(m_CurFrame != 0) //safety m_PrevFrame = cvCloneImage(m_CurFrame); IplImage *Frame=cvQueryFrame(m_Video); // get a frame of video //release the current frame of video before getting another ReleaseImage(&m_CurFrame); //now copy a permanant copy of it into the current frame m_CurFrame = cvCloneImage(Frame); if(m_PrevFrame == 0) return; // we need to bail until we have 2 frames //now convert this frame and the previous frame to grey ReleaseImage(&m_CurFrameGrey); m_CurFrameGrey = ConvertToGrey(m_CurFrame); IplImage * curgrey = m_CurFrameGrey; if(m_ReferenceGrey == 0) // no reference image set yet return; IplImage * prevgrey = ConvertToGrey(m_PrevFrame); //free and previous temporal image difference threshold picture ReleaseImage(&m_TemporalImage); //and create a new one m_TemporalImage = cvCreateImage(cvSize(curgrey->width,curgrey->height),curgrey->depth,1); //Zhang et al. [ZCS03] unsigned char *curdat,*prvdat,*tmpdat,*refdat; float mindat,maxdat,avdat,diff; curdat = (unsigned char *)curgrey->imageData; // the current frame prvdat = (unsigned char *)prevgrey->imageData; // the previous frame refdat = (unsigned char *)m_ReferenceGrey->imageData; // get a pointer to the greyscale reference image tmpdat = (unsigned char *)m_TemporalImage->imageData; // the temporal shadow difference image //build a map of the min / max values for(int y =0 ; y < m_CurFrame->height; y ++) { for(int x = 0; x < m_CurFrame->width; x++) { mindat = min(curdat[y * curgrey->widthStep + x],prvdat[y * prevgrey->widthStep + x]); maxdat = max(curdat[y * curgrey->widthStep + x],prvdat[y * prevgrey->widthStep + x]); avdat = (mindat + maxdat) / 2.0f; // find the dynamic midpoint threshold //diff = (float)refdat[y * m_ReferenceGrey->widthStep + x]; // get the pixel from the greyscale reference image diff = (float)curdat[y * curgrey->widthStep + x]; // get the pixel from the greyscale reference image diff = diff - avdat; // subtract the midpoint of the dynamic range from the difference in brightness between 2 successive frames of video //now, if diff is less than 0, (i.e. 0 crossing), then we have an edge // for the purposes of my algorithm ,we're going to make this stand out // by saying it's now 255 - (unsigned char)diff to make it really stand out // we may need to add in some translation or scaler to tweak this tmpdat[y * m_TemporalImage->widthStep + x] = (unsigned char)diff + offset; } } //remember to clean up ReleaseImage(&prevgrey); }
~WImageBufferC() { ReleaseImage(); }
/*************************************************************** *函数名称: TransProcess * * 参数: 无 * * * 返回值: unsigned int 处理结果 * * * 功能: 透明胶囊处理 * * ****************************************************************/ unsigned int CapsuleProc::TransProcess( WORKMODE mode ) { TTimeDiff td; td.Reset(); m_curData.Clear(); m_sortResult.NewPeriod(); m_sortObserver.NewPeriod(SortObserver::Mono); TImgDim dim = Dimension(); if (!m_transRemain.RemainCapsule(m_rawImage, dim, m_profileImage, m_remainParam, TTransRemain::TRANSMODE)) { OutputDebugString("RemainCapsule Failed!"); return 0; } m_sortObserver.ObserverIMG (SortObserver::MAll, m_cvbProfileImg); long profileBlobCount = 0; long minWidth = 1 + m_capsuleParam.capsuleDim.width/8; ProfileBlob (m_cvbProfileImg, minWidth, m_profileBlob); FBlobGetNumBlobs(m_profileBlob, profileBlobCount); int counter = 0; for(int i = 0; i < profileBlobCount; i++) { IMG proSubRot = NULL; size_t posIndex = 0; if(TransRotateIMG(i, m_profileBlob, m_cvbProfileImg, proSubRot, posIndex)) { if(posIndex ==0 || posIndex == 2) { counter++; } m_sortObserver.ObserverIMG(SortObserver::MSub, proSubRot); const long width = ImageWidth (proSubRot); const long height= ImageHeight(proSubRot); m_curData.WidthAndHeight(width, height); //all short if( IsCapsuleShort( height, width, m_capsuleParam.capsuleDim.height, m_capsuleParam.capsuleDim.tolerance)) { if (REALTIME == mode) { m_sortResult.SetResult(SortResult::ShortErr, posIndex); OutputDebugString("Capsule Short!"); ReleaseImage(proSubRot); continue; } } if (false == TransDoubleJudge(proSubRot, width/2)) { if (REALTIME == mode) { m_sortResult.SetResult(SortResult::DoubleEdge, posIndex); OutputDebugString("Double Edge!"); ReleaseImage(proSubRot); continue; } } //Is body/cap short long upEdge = 0, downEdge = 0; bool findEdge = FindUpDownEdge( proSubRot, m_segmentParam.edgeDensity, m_segmentParam.edgeThres, upEdge, downEdge); if (findEdge) { if (false == TransIsPartShort(height, upEdge, downEdge)) { if (REALTIME == mode) { m_sortResult.SetResult(SortResult::PartShort, posIndex); OutputDebugString("Part Short!"); ReleaseImage(proSubRot); continue; } } } //Soble Analysis if( false == TransSobelAnalysis(proSubRot)) { if (REALTIME == mode) { m_sortResult.SetResult(SortResult::SobelErr, posIndex); OutputDebugString("Sobel Edge!"); ReleaseImage(proSubRot); continue; } } //blobAnalysis if(false == TransBlobAnalysis( proSubRot, upEdge, downEdge, m_segmentParam.dynWndSize, m_segmentParam.dynThres, m_segmentParam.blobSize)) { if (REALTIME == mode) { m_sortResult.SetResult(SortResult::BlobErr, posIndex); ReleaseImage(proSubRot); OutputDebugString("BlobErr!"); continue; } } ReleaseImage(proSubRot); } } unsigned int badResult = m_sortResult.GetErrorPosition(); m_sortObserver.ObserverParam(SortObserver::MResult, badResult & 0x0F); m_sortObserver.ObserverParam(SortObserver::MTime, td.msec() ); if (mode == REALTIME) { if( eSecond == m_processIndex ) { CapsuleProc::AddToAllCount( counter ); } } if(profileBlobCount > 0) { RecordingImage(); } return badResult; }