void sendDecodeSequentialMode(void* storage){ TwoByte* st = (TwoByte*) storage; int h = workingImage.getHeight(); int w = workingImage.getWidth(); Byte* imgd = workingImage.getImageData(); unsigned int q = 1<<quantizationLevel; TwoByte* tblock = new TwoByte[192]; Byte* idctblock = new Byte[192]; for(int y = 0; y < h; y+=8){ for(int x = 0; x < w; x+=8){ for(int yy= 0; yy < 8; yy++){ for(int xx = 0; xx < 8; xx++){ int src = ((y + yy)*w + (x + xx))*3; int dest = (yy*8 + xx)*3; tblock[dest] = st[src]*q; tblock[dest + 1] = st[src + 1]*q; tblock[dest + 2] = st[src + 2]*q; } } doIDCT(tblock, idctblock); for(int yy= 0; yy < 8; yy++){ for(int xx = 0; xx < 8; xx++){ int dest = ((y + yy)*w + (x + xx))*3; int src = (yy*8 + xx)*3; imgd[dest] = idctblock[src]; imgd[dest + 1] = idctblock[src + 1]; imgd[dest + 2] = idctblock[src + 2]; } } drawImage(&workingImage, nextStart); Sleep(latency); } } delete[] tblock; delete[] idctblock; MessageBox(NULL, "DECODING DONE", "Status", NULL); }
void decodeProgressiveModeSpectralSelection(TwoByte* st){ int h = workingImage.getHeight(); int w = workingImage.getWidth(); Byte* imgd = workingImage.getImageData(); unsigned int q = 1<<quantizationLevel; TwoByte* tblock = new TwoByte[192]; Byte* idctblock = new Byte[192]; memset(tblock, 0x00, sizeof(TwoByte)*192); memset(idctblock, 0x00, sizeof(Byte)*192); for(int y = 0; y < h; y+=8){ for(int x = 0; x < w; x+=8){ for(int yy= 0; yy < 8; yy++){ for(int xx = 0; xx < 8; xx++){ int src = ((y + yy)*w + (x + xx))*3; int dest = (yy*8 + xx)*3; tblock[dest] = st[src]*q; tblock[dest + 1] = st[src + 1]*q; tblock[dest + 2] = st[src + 2]*q; } } doIDCT(tblock, idctblock); for(int yy= 0; yy < 8; yy++){ for(int xx = 0; xx < 8; xx++){ int dest = ((y + yy)*w + (x + xx))*3; int src = (yy*8 + xx)*3; imgd[dest] = idctblock[src]; imgd[dest + 1] = idctblock[src + 1]; imgd[dest + 2] = idctblock[src + 2]; } } } } delete[] tblock; delete[] idctblock; drawImage(&workingImage, nextStart); }
void encode(TwoByte *store){ int h = originalImage.getHeight(); int w = originalImage.getWidth(); Byte* imgd = originalImage.getImageData(); Byte* tblock = new Byte[192]; double* dctblock = new double[192]; memset(tblock, 0x00, sizeof(Byte)*192); memset(dctblock, 0x00, sizeof(double)*192); unsigned int q = 1<<quantizationLevel; for(int y = 0; y < h; y+=8){ for(int x = 0; x < w; x+=8){ for(int yy= 0; yy < 8; yy++){ for(int xx = 0; xx < 8; xx++){ int src = ((y+yy)*w + (x + xx))*3; int dest = (yy*8 + xx)*3; tblock[dest] = imgd[src]; tblock[dest + 1] = imgd[src + 1]; tblock[dest + 2] = imgd[src + 2]; } } doDCT(tblock, dctblock); for(int yy= 0; yy < 8; yy++){ for(int xx = 0; xx < 8; xx++){ int dest = ((y+yy)*w + (x + xx))*3; int src = (yy*8 + xx)*3; store[dest] = (TwoByte)(dctblock[src]/q); store[dest + 1] = (TwoByte)(dctblock[src + 1]/q); store[dest + 2] = (TwoByte)(dctblock[src + 2]/q); } } } } delete[] tblock; delete[] dctblock; }
/** @function main */ int main( int argc, char** argv ) { //300 350 double probability = 0.6; std::vector<string> sourceVector = saveFileName(argv[1]); std::vector<string> searchVector = saveFileName(argv[2]); double matched = 0; for(std::vector<string>::iterator sourceIterator = sourceVector.begin(); sourceIterator != sourceVector.end(); sourceIterator++ ){ for(std::vector<string>::iterator searchIterator = searchVector.begin(); searchIterator != searchVector.end(); searchIterator++ ){ if (*sourceIterator == *searchIterator) { matched++; break; } } } probability *= (matched / sourceVector.size()); MyImage *image = new MyImage(); image->setWidth(352); image->setHeight(288); image->setImagePath(argv[1]); if(!image->ReadImage()){ std::cout << "Error reading MyImage" << std::endl; } printf("%s", argv[1]); FILE *f = fopen(argv[1], "rb"); if (!f) { printf("error\n"); exit(1); } unsigned char pixels[352 * 288 * 3]; fread(pixels, sizeof(unsigned char), 352*288 * 3, f); fclose(f); cv::Mat object(Size(352, 288), CV_8UC3, pixels); namedWindow("image", CV_WINDOW_AUTOSIZE); imshow("image", object); if( !object.data ) { std::cout<< "Error reading object " << std::endl; return -1; } cv::Mat tmp, alpha; threshold(object, object, 255, 0, THRESH_TOZERO_INV); cv::Rect myROI(100, 100, 150, 150); int minHessian = 1000; cv::SurfFeatureDetector detector( minHessian ); std::vector<cv::KeyPoint> kp_object; detector.upright = false; detector.detect( object, kp_object ); cv::SurfDescriptorExtractor extractor; cv::Mat des_object; extractor.compute( object, kp_object, des_object ); cv::FlannBasedMatcher matcher; cv::namedWindow("Good Matches"); std::vector<cv::Point2f> obj_corners(4); //Get the corners from the object obj_corners[0] = cvPoint(0,0); obj_corners[1] = cvPoint( object.cols, 0 ); obj_corners[2] = cvPoint( object.cols, object.rows ); obj_corners[3] = cvPoint( 0, object.rows ); char key = 'a'; int framecount = 0; while (key != 27) { if (framecount < 5) { framecount++; continue; } cv::Mat des_image, img_matches; std::vector<cv::KeyPoint> kp_image; std::vector<std::vector<cv::DMatch > > matches; std::vector<cv::DMatch > good_matches; std::vector<cv::Point2f> obj; std::vector<cv::Point2f> scene; std::vector<cv::Point2f> scene_corners(4); cv::Mat H; MyImage *sceneMyPic = new MyImage(); sceneMyPic->setWidth(352); sceneMyPic->setHeight(288); sceneMyPic->setImagePath(argv[2]); sceneMyPic->ReadImage(); cv::Mat image(Size(352, 288), CV_8UC3, sceneMyPic->getImageData()); detector.detect( image, kp_image ); extractor.compute( image, kp_image, des_image ); matcher.knnMatch(des_object, des_image, matches, 2); for(int i = 0; i < cv::min(des_image.rows-1,(int) matches.size()); i++) //THIS LOOP IS SENSITIVE TO SEGFAULTS { if((matches[i][0].distance < 0.8*(matches[i][1].distance)) && ((int) matches[i].size()<=2 && (int) matches[i].size()>0)) { good_matches.push_back(matches[i][0]); } } drawMatches( object, kp_object, image, kp_image, good_matches, img_matches, cv::Scalar::all(-1), cv::Scalar::all(-1), std::vector<char>(), cv::DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS ); if (good_matches.size() >= 4) { Point2f averagePoint; for( int i = 0; i < good_matches.size(); i++ ) { obj.push_back( kp_object[ good_matches[i].queryIdx ].pt ); scene.push_back( kp_image[ good_matches[i].trainIdx ].pt ); averagePoint.x += kp_image[ good_matches[i].trainIdx ].pt.x; averagePoint.y += kp_image[ good_matches[i].trainIdx ].pt.y; } averagePoint.x /= good_matches.size(); averagePoint.y /= good_matches.size(); int inRange = 0; int delta = 60; for( int i = 0; i < good_matches.size(); i++ ){ int x =kp_image[ good_matches[i].trainIdx ].pt.x; int y =kp_image[ good_matches[i].trainIdx ].pt.y; if ((x > (averagePoint.x - delta) && x < (averagePoint.x + delta)) && (y > (averagePoint.y - delta) && (y < (averagePoint.y + delta)))) { inRange++; } } if (probability + (double)inRange / good_matches.size() > 0.8) { printf("found\n"); }else{ MyImage *objectPic = new MyImage(); objectPic->setWidth(352); objectPic->setHeight(288); objectPic->setImagePath(argv[1]); objectPic->ReadImage(); cv::Mat objectImage(Size(352, 288), CV_8UC3, objectPic->getImageData()); cv::Mat smallerQueryImage; resize(objectImage, smallerQueryImage, Size(16, 16), 0,0, INTER_CUBIC); cvtColor(objectImage, objectImage, COLOR_RGB2HSV); for (int x = 0; x < 352; x += 16) { for (int y = 0; y < 288; y += 16) { Rect region(Point(x, y), Size(16, 16)); cv::Mat subSampleOfScene = image(region); cvtColor(subSampleOfScene, subSampleOfScene, COLOR_RGB2HSV); int h_bins = 50; int s_bins = 60; int histSize[] = { h_bins, s_bins }; float h_ranges[] = { 0, 180 }; float s_ranges[] = { 0, 256 }; const float* ranges[] = { h_ranges, s_ranges }; int channels[] = { 0, 1 }; MatND hist_base; MatND hist_test; calcHist( &smallerQueryImage, 1, channels, Mat(), hist_base, 2, histSize, ranges, true, false ); normalize( hist_base, hist_base, 0, 1, NORM_MINMAX, -1, Mat() ); calcHist( &subSampleOfScene, 1, channels, Mat(), hist_test, 2, histSize, ranges, true, false ); normalize( hist_test, hist_test, 0, 1, NORM_MINMAX, -1, Mat() ); double base_test1 = compareHist(hist_base, hist_test, CV_COMP_CORREL); if (base_test1 > 0.1) { probability += base_test1; } } } if (probability > 0.8) { printf("found with confidence: %f\n", probability); } else{ printf("not found\n"); } } line(img_matches, cv::Point2f(averagePoint.x - 40 + sceneMyPic->getWidth(), averagePoint.y - 40), cv::Point2f(averagePoint.x + 40 + sceneMyPic->getWidth(), averagePoint.y - 40), cv::Scalar(0, 255, 0), 4); line(img_matches, cv::Point2f(averagePoint.x + 40 + sceneMyPic->getWidth(), averagePoint.y - 40), cv::Point2f(averagePoint.x + 40 + sceneMyPic->getWidth(), averagePoint.y + 40), cv::Scalar(0, 255, 0), 4); line(img_matches, cv::Point2f(averagePoint.x + 40 + sceneMyPic->getWidth(), averagePoint.y + 40), cv::Point2f(averagePoint.x - 40 + sceneMyPic->getWidth(), averagePoint.y + 40), cv::Scalar(0, 255, 0), 4); line(img_matches, cv::Point2f(averagePoint.x - 40 + sceneMyPic->getWidth(), averagePoint.y + 40), cv::Point2f(averagePoint.x - 40 + sceneMyPic->getWidth(), averagePoint.y - 40), cv::Scalar(0, 255, 0), 4); } imshow("Good matches", img_matches); key = cv::waitKey(0); } return 0; }