void get_hand_interval_2 (IplImage *body, int *interval) { CvMat *data, *labels, *means; int count; #define CLUSTERS 2 count = cvCountNonZero(body); data = cvCreateMat(count, 1, CV_32FC1); labels = cvCreateMat(count, 1, CV_32SC1); means = cvCreateMat(CLUSTERS, 1, CV_32FC1); fill_mat(body, data); cvKMeans2(data, CLUSTERS, labels, cvTermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 10, 10.0), 1, 0, 0, means, 0); double tmp; cvMinMaxLoc(body, &tmp, NULL, NULL, NULL, NULL); interval[0] = tmp; cvMinMaxLoc(means, &tmp, NULL, NULL, NULL, NULL); interval[1] = tmp; cvReleaseMat(&data); cvReleaseMat(&labels); }
void asef_locate_eyes(AsefEyeLocator *asef){ asef->face_image.cols = asef->face_rect.width; asef->face_image.rows = asef->face_rect.height; asef->face_image.type = CV_8UC1; asef->face_image.step = asef->face_rect.width; cvGetSubRect(asef->input_image, &asef->face_image, asef->face_rect); double xscale = ((double)asef->scaled_face_image_8uc1->cols)/((double)asef->face_image.cols); double yscale = ((double)asef->scaled_face_image_8uc1->rows)/((double)asef->face_image.rows); cvResize(&asef->face_image, asef->scaled_face_image_8uc1, CV_INTER_LINEAR); cvLUT(asef->scaled_face_image_8uc1, asef->scaled_face_image_32fc1, asef->lut); cvDFT(asef->scaled_face_image_32fc1, asef->scaled_face_image_32fc1, CV_DXT_FORWARD, 0); cvMulSpectrums(asef->scaled_face_image_32fc1, asef->lfilter_dft, asef->lcorr, CV_DXT_MUL_CONJ); cvMulSpectrums(asef->scaled_face_image_32fc1, asef->rfilter_dft, asef->rcorr, CV_DXT_MUL_CONJ); cvDFT(asef->lcorr, asef->lcorr, CV_DXT_INV_SCALE, 0); cvDFT(asef->rcorr, asef->rcorr, CV_DXT_INV_SCALE, 0); cvMinMaxLoc(asef->lroi, NULL, NULL, NULL, &asef->left_eye, NULL); cvMinMaxLoc(asef->rroi, NULL, NULL, NULL, &asef->right_eye, NULL); asef->left_eye.x = (asef->lrect.x + asef->left_eye.x)/xscale + asef->face_rect.x; asef->left_eye.y = (asef->lrect.y + asef->left_eye.y)/yscale + asef->face_rect.y; asef->right_eye.x = (asef->rrect.x + asef->right_eye.x)/xscale + asef->face_rect.x; asef->right_eye.y = (asef->rrect.y + asef->right_eye.y)/yscale + asef->face_rect.y; }
void asef_locate_eyes(AsefEyeLocator *asef, IplImage *image, CvRect face_rect, CvPoint *leye, CvPoint *reye){ asef->face_img.cols = face_rect.width; asef->face_img.rows = face_rect.height; asef->face_img.type = CV_8UC1; asef->face_img.step = face_rect.width; cvGetSubRect(image, &asef->face_img, face_rect); double xscale = ((double)asef->image_tile->cols)/((double)asef->face_img.cols); double yscale = ((double)asef->image_tile->rows)/((double)asef->face_img.rows); cvResize(&asef->face_img, asef->image_tile, CV_INTER_LINEAR); cvLUT(asef->image_tile, asef->image, asef->lut); cvDFT(asef->image, asef->image, CV_DXT_FORWARD, 0); cvMulSpectrums(asef->image, asef->lfilter_dft, asef->lcorr, CV_DXT_MUL_CONJ); cvMulSpectrums(asef->image, asef->rfilter_dft, asef->rcorr, CV_DXT_MUL_CONJ); cvDFT(asef->lcorr, asef->lcorr, CV_DXT_INV_SCALE, 0); cvDFT(asef->rcorr, asef->rcorr, CV_DXT_INV_SCALE, 0); cvMinMaxLoc(asef->lroi, NULL, NULL, NULL, leye, NULL); cvMinMaxLoc(asef->rroi, NULL, NULL, NULL, reye, NULL); leye->x = (asef->lrect.x + leye->x)/xscale + face_rect.x; leye->y = (asef->lrect.y + leye->y)/yscale + face_rect.y; reye->x = (asef->rrect.x + reye->x)/xscale + face_rect.x; reye->y = (asef->rrect.y + reye->y)/yscale + face_rect.y; }
void CvOneWayDescriptor::EstimatePose(IplImage* patch, int& pose_idx, float& distance) const { distance = 1e10; pose_idx = -1; CvRect roi = cvGetImageROI(patch); IplImage* patch_32f = cvCreateImage(cvSize(roi.width, roi.height), IPL_DEPTH_32F, patch->nChannels); float sum = cvSum(patch).val[0]; cvConvertScale(patch, patch_32f, 1/sum); for(int i = 0; i < m_pose_count; i++) { if(m_samples[i]->width != patch_32f->width || m_samples[i]->height != patch_32f->height) { continue; } float dist = cvNorm(m_samples[i], patch_32f); //float dist = 0.0f; //float i1,i2; //for (int y = 0; y<patch_32f->height; y++) // for (int x = 0; x< patch_32f->width; x++) // { // i1 = ((float*)(m_samples[i]->imageData + m_samples[i]->widthStep*y))[x]; // i2 = ((float*)(patch_32f->imageData + patch_32f->widthStep*y))[x]; // dist+= (i1-i2)*(i1-i2); // } if(dist < distance) { distance = dist; pose_idx = i; } #if 0 IplImage* img1 = cvCreateImage(cvSize(roi.width, roi.height), IPL_DEPTH_8U, 1); IplImage* img2 = cvCreateImage(cvSize(roi.width, roi.height), IPL_DEPTH_8U, 1); double maxval; cvMinMaxLoc(m_samples[i], 0, &maxval); cvConvertScale(m_samples[i], img1, 255.0/maxval); cvMinMaxLoc(patch_32f, 0, &maxval); cvConvertScale(patch_32f, img2, 255.0/maxval); cvNamedWindow("1", 1); cvShowImage("1", img1); cvNamedWindow("2", 1); cvShowImage("2", img2); printf("Distance = %f\n", dist); cvWaitKey(0); #endif } cvReleaseImage(&patch_32f); }
// Get an 8-bit equivalent of the 32-bit Float image. // Returns a new image, so remember to call 'cvReleaseImage()' on the result. IplImage* convertFloatImageToUcharImage(const IplImage *srcImg) { IplImage *dstImg = 0; if ((srcImg) && (srcImg->width > 0 && srcImg->height > 0)) { // Spread the 32bit floating point pixels to fit within 8bit pixel range. double minVal, maxVal; cvMinMaxLoc(srcImg, &minVal, &maxVal); //cout << "FloatImage:(minV=" << minVal << ", maxV=" << maxVal << ")." << endl; // Deal with NaN and extreme values, since the DFT seems to give some NaN results. if (cvIsNaN(minVal) || minVal < -1e30) minVal = -1e30; if (cvIsNaN(maxVal) || maxVal > 1e30) maxVal = 1e30; if (maxVal-minVal == 0.0f) maxVal = minVal + 0.001; // remove potential divide by zero errors. // Convert the format dstImg = cvCreateImage(cvSize(srcImg->width, srcImg->height), 8, 1); cvConvertScale(srcImg, dstImg, 255.0 / (maxVal - minVal), - minVal * 255.0 / (maxVal-minVal)); } return dstImg; }
static gboolean gst_motiondetect_apply ( IplImage * cvReferenceImage, const IplImage * cvCurrentImage, const IplImage * cvMaskImage, float noiseThreshold) { IplConvKernel *kernel = cvCreateStructuringElementEx (3, 3, 1, 1, CV_SHAPE_ELLIPSE, NULL); int threshold = (int)((1 - noiseThreshold) * 255); IplImage *cvAbsDiffImage = cvReferenceImage; double maxVal = -1.0; cvAbsDiff( cvReferenceImage, cvCurrentImage, cvAbsDiffImage ); cvThreshold (cvAbsDiffImage, cvAbsDiffImage, threshold, 255, CV_THRESH_BINARY); cvErode (cvAbsDiffImage, cvAbsDiffImage, kernel, 1); cvReleaseStructuringElement(&kernel); cvMinMaxLoc(cvAbsDiffImage, NULL, &maxVal, NULL, NULL, cvMaskImage ); if (maxVal > 0) { return TRUE; } else { return FALSE; } }
int main( int argc, char** argv ) { IplImage *src, *templ,*ftmp[6]; //ftmp will hold results CvPoint minloc[6], maxloc[6]; double minval[6], maxval[6]; int i; if( argc == 3){ //Read in the source image to be searched: if((src=cvLoadImage(argv[1], 1))== 0) { printf("Error on reading src image %s\n",argv[i]); return(-1); } //Read in the template to be used for matching: if((templ=cvLoadImage(argv[2], 1))== 0) { printf("Error on reading template %s\n",argv[2]); return(-1); } //ALLOCATE OUTPUT IMAGES: int iwidth = src->width - templ->width + 1; int iheight = src->height - templ->height + 1; for(i=0; i<6; ++i){ ftmp[i] = cvCreateImage( cvSize(iwidth,iheight),32,1); } //DO THE MATCHING OF THE TEMPLATE WITH THE IMAGE:218 | Chapter 7: Histograms and Matching Example 7-5. Template matching (continued) for(i=0; i<6; ++i){ cvMatchTemplate( src, templ, ftmp[i], i); //cvNormalize(ftmp[i],ftmp[i],1,0,CV_MINMAX); cvMinMaxLoc(ftmp[i], &minval[i], &maxval[i], &minloc[i], &maxloc[i], 0); std::cerr /*<< i << ":" << "minval: " << minval[i] \ << " maxval: " << maxval[i] */ \ << " minloc: " << minloc[i].x << ", " << minloc[i].y \ << " maxloc: " << maxloc[i].x << ", " << maxloc[i].y; std::cerr << "\n"; } //DISPLAY cvNamedWindow( "Template", 0 ); cvShowImage( "Template", templ ); cvNamedWindow( "Image", 0 ); cvShowImage( "Image", src ); cvNamedWindow( "SQDIFF", 0 ); cvShowImage( "SQDIFF", ftmp[0] ); cvNamedWindow( "SQDIFF_NORMED", 0 ); cvShowImage( "SQDIFF_NORMED", ftmp[1] ); cvNamedWindow( "CCORR", 0 ); cvShowImage( "CCORR", ftmp[2] ); cvNamedWindow( "CCORR_NORMED", 0 ); cvShowImage( "CCORR_NORMED", ftmp[3] ); cvNamedWindow( "CCOEFF", 0 ); cvShowImage( "CCOEFF", ftmp[4] ); cvNamedWindow( "CCOEFF_NORMED", 0 ); cvShowImage( "CCOEFF_NORMED", ftmp[5] ); //LET USER VIEW RESULTS: cvWaitKey(0); } else { printf("Call should be:" "matchTemplate image template \n"); } }
void LogMinMax(CvArr* mat,std::ostream& os) { //cvNormalize(gray,frame,1,0,CV_MINMAX); double m, M; cvMinMaxLoc(mat, &m, &M, NULL, NULL, NULL); os<<m<<"\t"<<M<<std::endl; }
double* ObtenerMaximo(IplImage* Imagen, STFrame* FrameData, CvRect Roi) { // obtener matriz de distancias normalizadas al background if (SHOW_VALIDATION_DATA == 1) printf(" \n\n Busqueda del máximo umbral..."); IplImage* IDif = 0; IplImage* peso = 0; CvSize size = cvSize(Imagen->width, Imagen->height); // get current frame size if (!IDif || IDif->width != size.width || IDif->height != size.height) { cvReleaseImage(&IDif); cvReleaseImage(&peso); IDif = cvCreateImage(cvSize(FrameData->BGModel->width, FrameData->BGModel->height), IPL_DEPTH_8U, 1); // imagen diferencia abs(I(pi)-u(p(i)) peso = cvCreateImage(cvSize(FrameData->BGModel->width, FrameData->BGModel->height), IPL_DEPTH_32F, 1);//Imagen resultado wi ( pesos) cvZero(IDif); cvZero(peso); } // |I(p)-u(p)|/0(p) cvAbsDiff(Imagen, FrameData->BGModel, IDif); cvDiv(IDif, FrameData->IDesvf, peso); // Buscar máximo double* Maximo = 0; cvMinMaxLoc(peso, Maximo, 0, 0, 0, FrameData->FG); return Maximo; }
int main(int argc, char** argv){ int i; if(argc != 3){ printf("Error 1: 2 arguments expected, %d given.\n",argc-1); return 0; } IplImage* source = cvLoadImage(argv[1],CV_LOAD_IMAGE_COLOR); IplImage* tmpl = cvLoadImage(argv[2],CV_LOAD_IMAGE_COLOR); int ww = source->width - tmpl->width + 1; int hh = source->height - tmpl->height + 1; IplImage *result = cvCreateImage(cvSize(ww,hh),IPL_DEPTH_32F, 1);//source->nChannels); cvMatchTemplate(source, tmpl, result, CV_TM_SQDIFF); CvPoint minLoc; CvPoint maxLoc; double minVal; double maxVal; cvMinMaxLoc( result, &minVal, &maxVal, &minLoc, &maxLoc, 0); cvRectangle(source, minLoc, cvPoint(minLoc.x+tmpl->width,minLoc.y+tmpl->height),cvScalar(0,0,255,1),1,8,0); cvNamedWindow("match",CV_WINDOW_AUTOSIZE); cvShowImage("match",source); cvWaitKey(0); cvReleaseImage(&source); cvReleaseImage(&tmpl); cvReleaseImage(&result); cvDestroyWindow("match"); }
void MatchTemplatePlugin::ProcessStatic ( int i, ImagePlus *img, ImagePlus *oimg, int method, CvSize winsize, IplImage* &map){ CvRect orect = cvBoundingRect(oimg->contourArray[i],1); RestrictRectLoc(orect, cvRect(0,0,img->orig->width,img->orig->height)); cvSetImageROI(oimg->orig, orect); CvRect rect = cvRect(MAX(0,orect.x-winsize.width), MAX(0,orect.y-winsize.height),orect.width+2*winsize.width, orect.height+2*winsize.height); rect.width = MIN(rect.width,oimg->orig->width-rect.x); rect.height = MIN(rect.height,oimg->orig->height-rect.y); cvSetImageROI(img->orig, rect); CvSize mapsize = MyPoint(MyPoint(rect)-MyPoint(orect)+wxPoint(1,1)).ToCvSize(); if (map && MyPoint(cvGetSize(map))!=MyPoint(mapsize)) cvReleaseImage(&map); if( !map ) map = cvCreateImage(mapsize, IPL_DEPTH_32F, 1); cvMatchTemplate( img->orig, oimg->orig, map, method ); cvResetImageROI(img->orig); cvResetImageROI(oimg->orig); CvPoint minloc; CvPoint maxloc; double minval, maxval; cvMinMaxLoc( map, &minval, &maxval, &minloc, &maxloc); bool minisbest = (method == CV_TM_SQDIFF || method==CV_TM_SQDIFF_NORMED); rect.x = rect.x + (minisbest ? minloc.x : maxloc.x); rect.y = rect.y + (minisbest ? minloc.y : maxloc.y); CvPoint shift = cvPoint(rect.x - orect.x, rect.y - orect.y); ShiftContour(oimg->contourArray[i],img->contourArray[i],shift); ShiftFeatPoints(oimg->feats[i], img->feats[i], cvPointTo32f(shift)); }
void thresholdCalculator::calculateAverages(ofxCvGrayscaleAdvanced & smallCurrentImg, ofxCvGrayscaleAdvanced & maskImg, ofRectangle & targetRect) { roi.x = targetRect.x / divisor; roi.y = targetRect.y / divisor; maskImg.setROI(roi); smallCurrentImg.setROI(roi); CvScalar tempPupilAvg = cvAvg(smallCurrentImg.getCvImage(), maskImg.getCvImage()); cvNot(maskImg.getCvImage(), notDiffImg.getCvImage()); pupilAvg = tempPupilAvg.val[0]; // get average of pupil black iteratively(get average twice) to remove the influence of glint cvThreshold(smallCurrentImg.getCvImage(), farFromAvg, pupilAvg + 30, 255, CV_THRESH_BINARY); // 30 is the distance from average. cvSub(maskImg.getCvImage(), farFromAvg, newMask); // make a mask to get rid of those far points. CvScalar newPupilAvg = cvAvg(smallCurrentImg.getCvImage(), newMask); // get new average value. // get average, min and max value of white area of an eye. CvScalar tempWhiteAvg = cvAvg(smallCurrentImg.getCvImage(), notDiffImg.getCvImage()); for (int i = 0; i < 6; i++) notDiffImg.erode(); // this might be very useful to reduce the influence of small noise & glint cvMinMaxLoc(smallCurrentImg.getCvImage(), &whiteMin, &whiteMax, &whiteLocMin, &whiteLocMax, notDiffImg.getCvImage()); maskImg.resetROI(); smallCurrentImg.resetROI(); pupilAvg = newPupilAvg.val[0]; // value is in the first element of CvScalar whiteAvg = tempWhiteAvg.val[0]; }
ofPoint matchFinder::getPoint() { // get the size for our result image CvSize result_size = cvSize(input.getWidth() - tpl.getWidth() + 1, input.getHeight() - tpl.getHeight() + 1); // create the result image for the comparison IplImage *result_image = cvCreateImage(result_size, IPL_DEPTH_32F, 1); // make the comparison cvMatchTemplate(input.getCvImage(), tpl.getCvImage(), result_image, CV_TM_SQDIFF); // copy to ofCv image. IplImage *result_char = cvCreateImage(cvSize(result_image->width, result_image->height), 8, 1); ofcv_result_image.allocate(result_size.width, result_size.height); ofcv_result_image = result_char; // get the location of the best match CvPoint min_loc; CvPoint max_loc; cvMinMaxLoc(result_image, &min_val, &max_val, &min_loc, &max_loc, 0); // clean up cvReleaseImage(&result_image); // return value ofPoint p = ofPoint(min_loc.x, min_loc.y); return p; }
DOUBLEVECT HoughAccumulator::FindBest() { DOUBLEVECT v; CvMat temp; CvMat* locMat = cvGetMat(acc, &temp, NULL, 1); // int rowsize = 4 * ((acc->dims / 4) + // ((acc->dims % 4 > 0) ? 1 : 0)); double max_val; CvPoint max_loc; cvMinMaxLoc(locMat, NULL, &max_val, NULL, &max_loc, NULL); int indraw = max_loc.x + max_loc.y * locMat->step; uchar* pValue = cvPtr2D(locMat, max_loc.y, max_loc.x); if (*pValue < 10) return v; indices[0] = indraw / acc->dim[0].step; indices[acc->dims - 1] = indraw % acc->dim[acc->dims - 2].step; for (int i = 1; i < acc->dims - 1; i ++) indices[i] = (indraw % acc->dim[i - 1].step) / acc->dim[i].step; for (int j = 0; j < acc->dims; j++) { double d = indices[j] / (float)precision + paramRanges[j].min; v.push_back(d); } return v; }
void Convert32FTo8U::execute() { CVImage* cvimg = cvImageIn.getBuffer(); if(!cvimg) { if(debug) std::cerr << getName() << "::ERROR::cvImageIn is NULL!\n"; cvImageOut.setBuffer(NULL); cvImageOut.out(); return; } if(cvimg->cvMatType != CV_32FC1) { if(debug) std::cerr << getName() << "::ERROR::cvImageIn has incorrect type (must be CV_32FC1)!\n"; cvImageOut.setBuffer(NULL); cvImageOut.out(); return; } if(!mp_cvimg8u){ //mp_cvimg32f = new CVImage(cvSize(cvimg->width, cvimg->height), CV_32FC1, 0); mp_cvimg8u = new CVImage(cvSize(cvimg->width, cvimg->height), CV_8UC1, 0); } IplImage* img = cvimg->ipl; double minval, maxval; cvMinMaxLoc(img, &minval, &maxval, NULL, NULL, NULL); double scale, shift; if(maxval == minval) { scale = 255.0; shift = 0.0; } else { scale = 255.0 / (maxval - minval); shift = - minval * scale; } cvConvertScale(img, mp_cvimg8u->ipl, scale, shift); cvImageOut.setBuffer(mp_cvimg8u); cvImageOut.out(); }
double template_original_match(IplImage* original_image,IplImage* template_image) { //resize the template and original sub image. //the scale difference 8 is concluded by repeated trials. //8*8 times matches will be done.The max value is more reasonable than resize the template and original with the same size and only do the matching once. IplImage* resized_original_image = cvCreateImage(cvSize(MATCHING_WIDTH + 8, MATCHING_HEIGHT + 8),original_image->depth,original_image-> nChannels); IplImage* resized_template_image = cvCreateImage(cvSize(MATCHING_WIDTH, MATCHING_HEIGHT),template_image->depth,template_image-> nChannels); IplImage* matching_result = cvCreateImage( cvSize(resized_original_image->width - resized_template_image -> width + 1,resized_original_image->height - resized_template_image->height + 1), IPL_DEPTH_32F, 1 ); double min_val; double max_val; CvPoint min_loc; CvPoint max_loc; cvResize(original_image,resized_original_image); cvResize(template_image,resized_template_image); //cvSmooth(resized_original_image,resized_original_image); //cvSmooth(resized_template_image,resized_template_image); //The match with max_val is the best match cvMatchTemplate(resized_original_image,resized_template_image,matching_result,CV_TM_CCOEFF_NORMED); cvMinMaxLoc(matching_result, &min_val, &max_val, &min_loc, &max_loc, NULL); return max_val; }
/** * Locate the user's eye with template matching * * @param IplImage* img the source image * @param IplImage* tpl the eye template * @param CvRect* window search within this window, * will be updated with the recent search window * @param CvRect* eye output parameter, will contain the current * location of user's eye * @return int '1' if found, '0' otherwise */ int locate_eye(IplImage* img, IplImage* tpl, CvRect* window, CvRect* eye) { IplImage* tm; CvRect win; CvPoint minloc, maxloc, point; double minval, maxval; int w, h; /* get the centroid of eye */ point = cvPoint( (*eye).x + (*eye).width / 2, (*eye).y + (*eye).height / 2 ); /* setup search window replace the predefined WIN_WIDTH and WIN_HEIGHT above for your convenient */ win = cvRect( point.x - WIN_WIDTH / 2, point.y - WIN_HEIGHT / 2, WIN_WIDTH, WIN_HEIGHT ); /* make sure that the search window is still within the frame */ if (win.x < 0) win.x = 0; if (win.y < 0) win.y = 0; if (win.x + win.width > img->width) win.x = img->width - win.width; if (win.y + win.height > img->height) win.y = img->height - win.height; /* create new image for template matching result where: width = W - w + 1, and height = H - h + 1 */ w = win.width - tpl->width + 1; h = win.height - tpl->height + 1; tm = cvCreateImage(cvSize(w, h), IPL_DEPTH_32F, 1); /* apply the search window */ cvSetImageROI(img, win); /* template matching */ cvMatchTemplate(img, tpl, tm, CV_TM_SQDIFF_NORMED); cvMinMaxLoc(tm, &minval, &maxval, &minloc, &maxloc, 0); /* release things */ cvResetImageROI(img); cvReleaseImage(&tm); /* only good matches */ if (minval > TM_THRESHOLD) return 0; /* return the search window */ *window = win; /* return eye location */ *eye = cvRect( win.x + minloc.x, win.y + minloc.y, TPL_WIDTH, TPL_HEIGHT ); return 1; }
//-------------------------------------------------------------------------------- void ofxCvGrayscaleImage::contrastStretch() { if( !bAllocated ){ ofLog(OF_LOG_ERROR, "in contrastStretch, image is not allocated"); return; } double minVal, maxVal; cvMinMaxLoc( cvImage, &minVal, &maxVal, NULL, NULL, 0 ); rangeMap( cvImage, minVal,maxVal, 0,255 ); flagImageChanged(); }
//-------------------------------------------------------------------------------- void ofxCvGrayscaleImage::contrastStretch() { if( !bAllocated ){ ofLogError("ofxCvGrayscaleImage") << "contrastStretch(): image not allocated"; return; } double minVal, maxVal; cvMinMaxLoc( cvImage, &minVal, &maxVal, NULL, NULL, 0 ); rangeMap( cvImage, minVal,maxVal, 0,255 ); flagImageChanged(); }
void FacePredict::FaceSynthesis(AAM_Shape &shape, CvMat* texture, IplImage* newImage) { double thisfacewidth = shape.GetWidth(); shape.Scale(stdwidth / thisfacewidth); shape.Translate(-shape.MinX(), -shape.MinY()); AAM_PAW paw; CvMat* points = cvCreateMat (1, __shape.nPoints(), CV_32FC2); CvMemStorage* storage = cvCreateMemStorage(0); paw.Train(shape, points, storage, __paw.GetTri(), false); //the actual shape __AAMRefShape.Translate(-__AAMRefShape.MinX(), -__AAMRefShape.MinY()); //refShape, central point is at (0,0);translate the min to (0,0) double minV, maxV; cvMinMaxLoc(texture, &minV, &maxV); cvConvertScale(texture, texture, 1/(maxV-minV)*255, -minV*255/(maxV-minV)); cvZero(newImage); int x1, x2, y1, y2, idx1 = 0, idx2 = 0; int tri_idx, v1, v2, v3; int minx, miny, maxx, maxy; minx = shape.MinX(); miny = shape.MinY(); maxx = shape.MaxX(); maxy = shape.MaxY(); for(int y = miny; y < maxy; y++) { y1 = y-miny; for(int x = minx; x < maxx; x++) { x1 = x-minx; idx1 = paw.Rect(y1, x1); if(idx1 >= 0) { tri_idx = paw.PixTri(idx1); v1 = paw.Tri(tri_idx, 0); v2 = paw.Tri(tri_idx, 1); v3 = paw.Tri(tri_idx, 2); x2 = paw.Alpha(idx1)*__AAMRefShape[v1].x + paw.Belta(idx1)*__AAMRefShape[v2].x + paw.Gamma(idx1)*__AAMRefShape[v3].x; y2 = paw.Alpha(idx1)*__AAMRefShape[v1].y + paw.Belta(idx1)*__AAMRefShape[v2].y + paw.Gamma(idx1)*__AAMRefShape[v3].y; idx2 = __paw.Rect(y2, x2); if(idx2 < 0) continue; CV_IMAGE_ELEM(newImage, byte, y, 3*x) = cvmGet(texture, 0, 3*idx2); CV_IMAGE_ELEM(newImage, byte, y, 3*x+1) = cvmGet(texture, 0, 3*idx2+1); CV_IMAGE_ELEM(newImage, byte, y, 3*x+2) = cvmGet(texture, 0, 3*idx2+2); } } } cvReleaseMat(&points); cvReleaseMemStorage(&storage); }
int main() { IplImage* img=cvLoadImage("3.7.jpg"); if(!img) { printf("Connot open the image\n"); return -1; } IplImage* rImg=cvCreateImage(cvGetSize(img),img->depth,1); IplImage* gImg=cvCreateImage(cvGetSize(img),img->depth,1); IplImage* bImg=cvCreateImage(cvGetSize(img),img->depth,1); IplImage* clone1=cvCreateImage(cvGetSize(img),img->depth,1); IplImage* clone2=cvCreateImage(cvGetSize(img),img->depth,1); double green_maxPixel=0; double green_minPixel=0; double thresh; cvSplit(img,rImg,gImg,bImg,0); cvNamedWindow("GreenImage0"); cvShowImage("GreenImage0",gImg); cvCopy(gImg,clone1); cvCopy(gImg,clone2); cvMinMaxLoc(gImg,&green_maxPixel,&green_minPixel); thresh=(unsigned char)(green_maxPixel-green_minPixel)/2.0; cvSet(clone1,cvScalar(thresh)); cvZero(clone2); cvCmp(gImg,clone1,clone2,CV_CMP_GE);//掩码标记 cvSubS(gImg,cvScalar(thresh/2),gImg,clone2);//两个矩阵相减 cvNamedWindow("GreenImage1"); cvShowImage("GreenImage1",gImg); cvWaitKey(0); cvReleaseImage(&img); cvReleaseImage(&rImg); cvReleaseImage(&gImg); cvReleaseImage(&bImg); cvDestroyWindow("GreenImage0"); cvDestroyWindow("GreenImage1"); return 0; }
void CueTemplate::adapt() { if(!m_init) return; CVImage* cvgrayimg = cvGrayImageIn.getBuffer(); if(!cvgrayimg) { std::cerr<< getName() << "::ERROR::execute()::cvGrayImageIn is NULL!...\n"; return; } IplImage* grayimg = cvgrayimg->ipl; TrackData* track = trackIn.getBuffer(); // if(!track){ std::cerr<< getName() << "::ERROR::execute()::trackIn is NULL!...\n"; return; } float rel; CvPoint winner; if(track) { rel = track->reliability; winner = track->winnerPos; } else{ double min, max; CvPoint minLoc, maxLoc; cvMinMaxLoc(mp_boundedoutputimg, &min, &max, &minLoc, &maxLoc, NULL); rel = (float)max; winner = maxLoc; } if(rel > m_threshold){ // adapt toward new template int x = winner.x; int y = winner.y; if(x < m_halftemplatesizex) x = m_halftemplatesizex; if(y < m_halftemplatesizey) y = m_halftemplatesizey; if(x >= grayimg->width - m_halftemplatesizex) x = grayimg->width - m_halftemplatesizex-1; if(y >= grayimg->height - m_halftemplatesizey) y = grayimg->height - m_halftemplatesizey-1; CvRect rect; rect.x = x - m_halftemplatesizex; rect.y = y - m_halftemplatesizey; rect.width = m_templatesizex; rect.height = m_templatesizey; cvSetImageROI(grayimg, rect ); cvCopy( grayimg, mp_newtemplateimg ); cvScale( mp_templateimg, mp_templateimg, 1.0 - m_tfacs); cvScale( mp_newtemplateimg, mp_newtemplateimg, m_tfacs); cvAdd( mp_newtemplateimg, mp_templateimg, mp_templateimg ); cvResetImageROI( grayimg ); cvTemplateImageOut.out(); } else{ // adapting back to the original template cvScale( mp_templateimg, mp_templateimg, 1.0 - (m_tfacs/m_back) ); cvScale( mp_origtemplateimg, mp_temptemplateimg, (m_tfacs/m_back) ); cvAdd( mp_temptemplateimg, mp_templateimg, mp_templateimg ); cvTemplateImageOut.out(); } }
//------FUNCTION USED TO DETERMINE DENOMINATION---------- void detectDenomination(IplImage *img,int thresx,int thresy) { CvPoint minloc, maxloc; double minval, maxval; float max=-1; int index=-1; //--------4 TEMPLATES NOTES OF NoteFinal---------- for(int i=0; i<4;i++) { //IplImage *tpl=cvLoadImage(Gandhitpl[i]); IplImage *source = cvLoadImage(NoteFinal[i]); //declare a destination IplImage object with correct size, depth and channels IplImage *destination = cvCreateImage ( cvSize( thresx,thresy),source->depth, source->nChannels ); //use cvResize to resize source to a destination image cvResize(source, destination); IplImage *tpl=destination; int res_width=img->width-tpl->width+1; int res_height=img->height-tpl->height+1; IplImage *res = cvCreateImage( cvSize( res_width, res_height ), IPL_DEPTH_32F, 1 ); /* choose template matching method to be used */ //cvMatchTemplate( img, tpl, res, CV_TM_SQDIFF ); //cvMatchTemplate( img, tpl, res, CV_TM_SQDIFF_NORMED ); //cvMatchTemplate( img, tpl, res, CV_TM_CCORR ); //cvMatchTemplate( img, tpl, res, CV_TM_CCORR_NORMED ); //cvMatchTemplate( img, tpl, res, CV_TM_CCOEFF ); cvMatchTemplate( img, tpl, res, CV_TM_CCOEFF_NORMED ); cvMinMaxLoc( res, &minval, &maxval,&minloc,&maxloc, 0); //cvNamedWindow("showtplmatch",1); //cvShowImage("showtplmatch",img); // printf("\n i= %d, max = %f",i,maxval); // cvWaitKey(100); if(max<maxval) { max=maxval; index=i; } } // ----- THE TPL NOTE FOR WHICH THE MATCH WAS MAXIMUM , THAT INDEX COUNT IS INCREMENTED maxnote[index]++; }
static void writeNormalized( const char* filename, const CvMat* img ) { double minval, maxval; CvPoint minpoint, maxpoint; cvMinMaxLoc( img, &minval, &maxval, &minpoint, &maxpoint, NULL ); CvMat* scaled_img = cvCreateMat( img->rows, img->cols, CV_8UC1); cvConvertScale( img, scaled_img, 255.0/(maxval - minval), -minval*255.0/(maxval - minval) ); cvSaveImage( filename, scaled_img, 0 ); }
void THISCLASS::OnStep() { // Get the input image IplImage* inputimage = mCore->mDataStructureImageGray.mImage; if (! inputimage) { AddError(wxT("No image on selected input.")); return; } // Calculate non-zero elements if (mCalculateNonZero) { int non_zero= cvCountNonZero(inputimage); CommunicationMessage m(wxT("STATS_NONZERO")); m.AddInt(non_zero); mCore->mCommunicationInterface->Send(&m); } // Calculate sum if (mCalculateSum) { CvScalar sum= cvSum(inputimage); CommunicationMessage m(wxT("STATS_SUM")); m.AddDouble(sum.val[0]); mCore->mCommunicationInterface->Send(&m); } // Calculate mean and standard deviation if (mCalculateMeanStdDev) { CvScalar mean; CvScalar std_dev; cvAvgSdv(inputimage, &mean, &std_dev, NULL); CommunicationMessage m(wxT("STATS_MEANSTDDEV")); m.AddDouble(mean.val[0]); m.AddDouble(std_dev.val[0]); mCore->mCommunicationInterface->Send(&m); } // Calculate min and max if (mCalculateMinMax) { double min_val; double max_val; cvMinMaxLoc(inputimage, &min_val, &max_val, NULL, NULL, NULL); CommunicationMessage m(wxT("STATS_MINMAX")); m.AddDouble(min_val); m.AddDouble(max_val); mCore->mCommunicationInterface->Send(&m); } // Set the display DisplayEditor de(&mDisplayOutput); if (de.IsActive()) { de.SetMainImage(inputimage); } }
double comparison::run_template_matching_comparison(int type){ int height = src2->height - src1->height +1; int width = src2->width - src1->width +1; /* create a new image, to store phase correlation result */ IplImage *ftmp = cvCreateImage( cvSize(width,height ), IPL_DEPTH_32F, 1); /* get phase correlation of input images */ cvMatchTemplate(src2,src1,ftmp,type); /* find the maximum value and its location */ CvPoint minloc, maxloc; double minval, maxval; cvMinMaxLoc(ftmp, &minval, &maxval, &minloc, &maxloc, 0 ); return maxval; }
void TemlateBit(IplImage* dest,const char *filename,CvPoint *ptDest,CvPoint *ptSrc){ //IplImage *img; IplImage *tpl; IplImage *res; CvPoint minloc,maxloc; double minval,maxval; int img_width,img_height; int tpl_width,tpl_height; int res_width,res_height; if (0==dest) { return ; } tpl=cvLoadImage(filename,CV_LOAD_IMAGE_COLOR); if (0==tpl) { return ; } img_width=dest->width; img_height=dest->height; tpl_width=tpl->width; tpl_height=tpl->height; res_width=img_width-tpl_width+1; res_height=img_height-tpl_height+1; res=cvCreateImage(cvSize(res_width,res_height),IPL_DEPTH_32F,1); cvMatchTemplate(dest,tpl,res,CV_TM_SQDIFF); cvMinMaxLoc(res,&minval,&maxval,&minloc,&maxloc,0); //cvRectangle(dest,cvPoint(minloc.x/2,minloc.y/2),cvPoint(minloc.x/2+tpl_width/2,minloc.y/2+tpl_height/2),cvScalar(0,0,255,0),1,0,0); //ptDest=cvPoint(minloc.x,minloc.y); //ptSrc=cvPoint(minloc.x+tpl_width,minloc.y+tpl_height); ptDest->x=minloc.x; ptDest->y=minloc.y; ptSrc->x=minloc.x+tpl_width; ptSrc->y=minloc.y+tpl_height; //printf("ptDest.x=%d ptDest.y=%d \n ptSrc.x=%d ptSrc.y=%d \n",ptDest.x,ptDest.y,ptSrc.x,ptSrc.y); //cvNamedWindow("reference",CV_WINDOW_AUTOSIZE); //cvNamedWindow("template",CV_WINDOW_AUTOSIZE); //cvShowImage("reference",dest); //cvShowImage("template",tpl); //cvWaitKey(0); //cvDestroyWindow("reference"); //cvDestroyWindow("template"); //cvReleaseImage(&dest); cvReleaseImage(&tpl); cvReleaseImage(&res); }
//形态学重建应用2:空洞的填充 void lhMorpFillHole(const IplImage* src, IplImage* dst) { IplImage *temp = cvCloneImage(src); double min, max; cvMinMaxLoc(src, &min, &max); //标记图像 cvRectangle(temp, cvPoint(3,3), cvPoint(temp->width-7, temp->height-7), CV_RGB(max,max,max), -1); //将原图像作为掩模图像 lhMorpRErode(temp, src, dst); cvReleaseImage(&temp); //cvSub(src, dst, dst); }
/*! * \biref Initial step of the K-means algorithm. * * The initial clusters means are initializied with the minimum and * maximum depth value of the body. * * \param[in] vector of data * \param[in,out] vecotor of cluster's means * \param[in,out] vecotr of weights (number of elements per cluster) * \return the min depth value */ static int init_step (CvMat *data, CvMat *means, CvMat *weights) { double init[2]; cvMinMaxLoc(data, &(init[0]), &(init[1]), NULL, NULL, NULL); int i; for (i=0; i<K; i++) { cvmSet(means, i, 0, init[i]); cvmSet(weights, i, 0, 1); } return (int)init[0]; }
void gandhijitplMatch(GandhitplMatch detectedimg,int index) { CvPoint minloc, maxloc; double minval, maxval; for(int i=0; i<4;i++) { //IplImage *tpl=cvLoadImage(Gandhitpl[i]); IplImage *source = cvLoadImage(Gandhitpl[i]); printf("gandhitplmatchloaded"); //declare a destination IplImage object with correct size, depth and channels IplImage *destination = cvCreateImage ( cvSize(detectedimg.width ,detectedimg.height),source->depth, source->nChannels ); //use cvResize to resize source to a destination image cvResize(source, destination); IplImage *tpl=destination; int res_width=detectedimg.faceimg->width-tpl->width+1; int res_height=detectedimg.faceimg->height-tpl->height+1; IplImage *res = cvCreateImage( cvSize( res_width, res_height ), IPL_DEPTH_32F, 1 ); /* choose template matching method to be used */ //cvMatchTemplate( img, tpl, res, CV_TM_SQDIFF ); //cvMatchTemplate( img, tpl, res, CV_TM_SQDIFF_NORMED ); //cvMatchTemplate( img, tpl, res, CV_TM_CCORR ); //cvMatchTemplate( img, tpl, res, CV_TM_CCORR_NORMED ); //cvMatchTemplate( img, tpl, res, CV_TM_CCOEFF ); cvMatchTemplate( detectedimg.faceimg, tpl, res, CV_TM_CCOEFF_NORMED ); cvMinMaxLoc( res, &minval, &maxval,&minloc,&maxloc, 0); cvNamedWindow("showtplmatch",1); cvShowImage("showtplmatch",detectedimg.faceimg); // printf("\n i= %d, max = %f",i,maxval); // cvWaitKey(100); if(globalmaximum<maxval) { globalmaximum=maxval; globalmaxindex=index; } } }