int test_rgb888_to_yuv(const unsigned char *src, int w, int h) { int lRetVal = 0; apexcv::ColorConverter rgb; DataDescriptor srcImg; srcImg.InitManual(w/*/4*/, h, (void *)src, (void *)OAL_MemoryReturnAddress((void *)src, ACCESS_PHY), DATATYPE_08U, 4, 1); DataDescriptor dstImg(w/*/4*/, h, DATATYPE_08U, 4, 1); DataDescriptor dstRefImg(w/*/4*/, h, DATATYPE_08U, 4, 1); if(srcImg.IsOK() && dstImg.IsOK() && dstRefImg.IsOK()) { //run implementation lRetVal |= rgb.convert(srcImg, dstImg, ColorConverter::RGB888_TO_YUV); //obtain dst data pointers unsigned char* pDst = (unsigned char*) dstImg.GetDataPtr(); unsigned char* pDstRef = (unsigned char*) dstRefImg.GetDataPtr(); //run reference rgb888_to_yuv_ref((uint8_t*)pDstRef, w*4, (uint8_t*)src, w*4, w, h); if (dstImg == dstRefImg) lRetVal |= 0; else lRetVal |= 1; } else { lRetVal |= 1; } //free buffers dstImg.SetFreeOnExit(true); dstRefImg.SetFreeOnExit(true); return lRetVal; }
int test_rgb565_to_rgb888(const unsigned short *src, int w, int h) { int lRetVal = 0; apexcv::ColorConverter rgb; DataDescriptor srcImg; srcImg.InitManual(w, h, (void *)src, (void *)OAL_MemoryReturnAddress((void *)src, ACCESS_PHY), DATATYPE_16U); DataDescriptor dstImg(w, h, DATATYPE_32U); DataDescriptor dstRefImg(w, h, DATATYPE_32U); if(srcImg.IsOK() && dstImg.IsOK() && dstRefImg.IsOK()) { //run implementation lRetVal |= rgb.convert(srcImg, dstImg, apexcv::ColorConverter::RGB565_TO_RGB888); //obtain dst data pointers unsigned int* pDst = (unsigned int*) dstImg.GetDataPtr(); unsigned int* pDstRef = (unsigned int*) dstRefImg.GetDataPtr(); //run reference rgb565_to_rgb888_ref(pDstRef, w, src, w, w, h); if (dstImg == dstRefImg) lRetVal |= 0; else lRetVal |= 1; } else { lRetVal |= 1; } //free buffers dstImg.SetFreeOnExit(true); dstRefImg.SetFreeOnExit(true); return lRetVal; }
Mat CmIllustr::ArrangeImgs(vecM &imgs, vecD &len, int W, int H, bool toRow) { int imgN = (int)(imgs.size()), s = 0; CV_Assert(len.size() == imgN); double ratio, sumL = 0, err = 0; for (int i = 0; i < imgN; i++) sumL += len[i]; ratio = ((toRow ? W : H) - (imgN - 1) * space) / sumL; Mat dstImg(H, W, CV_8UC3); dstImg = Scalar(255, 255, 255); for (int i = 0; i < imgN; i++) { len[i] *= ratio; int l = cvRound(len[i] + err); Rect reg = toRow ? Rect(s, 0, l, H) : Rect(0, s, W, l); resize(imgs[i], dstImg(reg), reg.size()); err = len[i] + err - l; s += l + space; } CV_Assert(s - space == (toRow ? dstImg.cols : dstImg.rows)); return dstImg; }
Image RDC::compensate(const Image& srcImg) { Image dstImg(srcImg.width, srcImg.height); for (int i = 0; i < srcImg.width; i++) { for (int j = 0 ; j < srcImg.height; j++) { float newPix = 0; //Simple fixed threshold method if (srcImg.pixelAt(i,j) > 0.5) { newPix = 255; } dstImg.pixelWrite(newPix, i, j); } } return dstImg; //TODO compensate #1 test with dummy matrix }
int test_ht_8UC3_to_y(const uint8_t *src, int w, int h, uint8_t KA, uint8_t KB, uint8_t KC, uint16_t KR) { int lRetVal = 0; apexcv::OPT::ColorConverter rgb; DataDescriptor srcImg; srcImg.InitManual(w/*/4*/, h, (void *)src, (void *)OAL_MemoryReturnAddress((void *)src, ACCESS_PHY), DATATYPE_08U, 4, 1); DataDescriptor dstImg(w/*/4*/, h, DATATYPE_16S); DataDescriptor dstRefImg(w/*/4*/, h, DATATYPE_16S); if(srcImg.IsOK() && dstImg.IsOK() && dstRefImg.IsOK()) { //run implementation lRetVal |= rgb.convert(srcImg, dstImg, OPT::ColorConverter::HT_8UC3_TO_Y, KA, KB, KC, KR); //obtain dst data pointers int16_t* pDst = (int16_t*) dstImg.GetDataPtr(); int16_t* pDstRef = (int16_t*) dstRefImg.GetDataPtr(); //run reference csc_8UC3_to_y_ref((int16_t*)pDstRef, w, (uint8_t*)src, w*4, w, h, KA, KB, KC, KR); if (dstImg == dstRefImg) lRetVal |= 0; else lRetVal |= 1; } else { lRetVal |= 1; } //free buffers dstImg.SetFreeOnExit(true); dstRefImg.SetFreeOnExit(true); return lRetVal; }
int test_integral_image(const unsigned char *src, int w, int h) { int lRetVal = 0; apexcv::IntegralImage i; DataDescriptor srcImg; srcImg.InitManual(w, h, (void *)src, (void *)OAL_MemoryReturnAddress((void *)src, ACCESS_PHY), DATATYPE_08U); DataDescriptor dstImg(w, h, DATATYPE_32U); DataDescriptor dstRefImg(w, h, DATATYPE_32U); if(srcImg.IsOK() &&dstImg.IsOK() && dstRefImg.IsOK()) { //run implementation lRetVal |= i.exec(srcImg, dstImg); //obtain dst data pointers unsigned int* pDst = (unsigned int*) dstImg.GetDataPtr(); unsigned int* pDstRef = (unsigned int*) dstRefImg.GetDataPtr(); //run reference integral_image_ref(pDstRef, src, w, w, h); if (dstImg == dstRefImg) lRetVal |= 0; else lRetVal |= 1; } else { lRetVal |= 1; } //free buffers dstImg.SetFreeOnExit(true); dstRefImg.SetFreeOnExit(true); return lRetVal; }
int Judgement::JudgementYON(Mat &image) { int success = 0; MatND dstHist; Mat histoImg = image.clone(); calcHist(&histoImg, 1, &channels, Mat(), dstHist, 1, &size, ranges); Mat dstImg(256, 256, CV_8U, Scalar(0));//画直方图 double minValue = 0; double maxValue = 0; Point maxloc; minMaxLoc(dstHist, &minValue, &maxValue, NULL, &maxloc); //cout << " " << n << "." << m << " " << maxValue << endl; int hpt = saturate_cast<int>(0.9 * 256); vector<int> Boundnum; for (int j = 0; j < 256; j++) { float binValue = dstHist.at<float>(j); int realValue = saturate_cast<int>(binValue * hpt / maxValue); if (realValue != 0) { rectangle(dstImg, Point(j, 255), Point(j, 256 - realValue), Scalar(255)); Boundnum.push_back(j); } } int maxdata = *max_element(Boundnum.begin(), Boundnum.end()); int mindata = *min_element(Boundnum.begin(), Boundnum.end());//寻找直方图动态范围 Rect recttemp; recttemp.x = maxloc.x; recttemp.y = maxloc.y - int((maxdata - mindata)*0.15); recttemp.width = 1; recttemp.height = int((maxdata - mindata)*0.3); rectangle(dstHist, recttemp, Scalar(0), -1); minMaxLoc(dstHist, &minValue, &maxValue, NULL, &maxloc); int anoThres = maxloc.y;//寻找次峰值 Scalar avgnum; Mat StdDevImg; meanStdDev(histoImg, avgnum, StdDevImg); double Stdnum = StdDevImg.at<double>(Point(0, 0)); int ThreStep = maxdata - mindata; int StepNum = 30; int OrStep = mindata + int(ThreStep / 10); int Dstep = int(ThreStep / 30.0 + 0.5); if (Dstep == 0) { Dstep = 1; StepNum = ThreStep; } Mat TempImg; histoImg.copyTo(TempImg); vector<vector<Point>> contours; vector<Vec4i> hierarchy; Point pointSN, maxPoint = Point(0, 0); int Marknumone = 0; int Marknumtwo = 0; int Marknumthree = 0; for (int i = 0; i < StepNum; i++) { vector<Point> SN; OrStep = OrStep + Dstep; threshold(histoImg, TempImg, OrStep, 255, CV_THRESH_BINARY); /*Mat element = getStructuringElement(MORPH_RECT,Size(2,2)); erode(TempImg, TempImg, cv::Mat()); dilate(TempImg, TempImg, cv::Mat());*/ TempImg = ~TempImg; /*stringstream strstrone; strstrone << "水渍动态图" << i << ".jpg"; imwrite(strstrone.str(), TempImg);*/ Mat BoundImg(TempImg.rows, TempImg.cols, CV_8UC1, Scalar(255)); Rect Wrect; Wrect.x = 1; Wrect.y = 1; Wrect.width = BoundImg.cols - 2; Wrect.height = BoundImg.rows - 2; rectangle(BoundImg, Wrect, Scalar(0), -1); Mat PlusImg(TempImg.rows + 2, TempImg.cols + 2, CV_8UC1, Scalar(255)); Mat PlusROI = PlusImg(Rect(1, 1, TempImg.cols, TempImg.rows)); TempImg.copyTo(PlusROI); Mat ContoursImg = PlusImg.clone(); findContours(ContoursImg, contours, hierarchy, RETR_TREE, CV_CHAIN_APPROX_SIMPLE); for (size_t j = 0; j < contours.size(); j++) { double area = cv::contourArea(contours[j]); pointSN.x = int(area); pointSN.y = j; SN.push_back(pointSN); } if (contours.size() != 0) { sort(SN.begin(), SN.end(), SortByM2); maxPoint = SN.back(); if (OrStep > anoThres - 5 && OrStep<anoThres + 20) Dstep = 1; else { Dstep = int(ThreStep / 30.0 + 0.5); } if (Dstep == 0) Dstep = 1; int k = maxPoint.y; Mat MarkImg(TempImg.rows, TempImg.cols, CV_8UC1, Scalar(0)); drawContours(MarkImg, contours, k, Scalar(255), -1); bitwise_and(BoundImg, MarkImg, MarkImg); int Mbound = 0;//判断轮廓是否到边界 Mbound = countNonZero(MarkImg); if (Mbound>0.5*(histoImg.cols)) break; if (contours[k].size() <= 4) continue; int son = hierarchy[k][2]; Point gravitycore = barycenter(contours[k]);//寻找轮廓重心 Rect maxcontours = boundingRect(contours[k]); int wValue = maxcontours.width / 12; gravitycore = gravitycore + Point(wValue - 1, wValue - 1); Mat gravityImg(TempImg.rows + 2 * wValue, TempImg.cols + 2 * wValue, CV_8UC1, Scalar(0)); Mat gravityImgROI = gravityImg(Rect(wValue, wValue, TempImg.cols, TempImg.rows)); TempImg.copyTo(gravityImgROI); Rect gravityrect = Rect(gravitycore - Point(1, 1), gravitycore + Point(2 * wValue, 2 * wValue) - Point(2, 2));//画出重心周围(2 * wValue)*(2 * wValue)的矩形区域 if (gravityrect.x < 0 || gravityrect.y < 0) continue; int avnum = countNonZero(gravityImg(Rect(gravityrect))); vector<Point> hull; convexHull(contours[k], hull, false); double promark = (contourArea(contours[k])) / (contourArea(hull)); if (son >= 0)//判断是否为父轮廓 { int sonarea = 0; for (size_t j = 0; j < contours.size(); j++) { if (hierarchy[j][3] == k&&contourArea(contours[j])>4.0) sonarea = sonarea + contourArea(contours[j]); } if (50 * sonarea>maxPoint.x)//此处忽略一些偶然出现的中空点 Marknumone++; } if (avnum < double(0.5 * gravityrect.width*gravityrect.width))//在重心区域中的白色点的数量是否过半 Marknumtwo++; if (promark < 0.6) Marknumthree++; } } if (Marknumone > 2 || Marknumtwo >= 2 || Marknumthree > 3)//缺陷点也可能偶然出现包含 { /*cout << "该点是水渍2" << endl;*/ } else { /*cout << "该点是缺陷2" << endl;*/ success++; } return success; }
/* * aSrc is the current frame being drawn, * aDst is the composition frame where the current frame is drawn into. * aSrcRect is the size of the current frame, and the position of that frame * in the composition frame. */ nsresult imgContainer::DrawFrameTo(gfxIImageFrame *aSrc, gfxIImageFrame *aDst, nsIntRect& aSrcRect) { NS_ENSURE_ARG_POINTER(aSrc); NS_ENSURE_ARG_POINTER(aDst); nsIntRect dstRect; aDst->GetRect(dstRect); // According to both AGIF and APNG specs, offsets are unsigned if (aSrcRect.x < 0 || aSrcRect.y < 0) { NS_WARNING("imgContainer::DrawFrameTo: negative offsets not allowed"); return NS_ERROR_FAILURE; } // Outside the destination frame, skip it if ((aSrcRect.x > dstRect.width) || (aSrcRect.y > dstRect.height)) { return NS_OK; } gfx_format format; aSrc->GetFormat(&format); if (format == gfxIFormats::PAL || format == gfxIFormats::PAL_A1) { // Larger than the destination frame, clip it PRInt32 width = PR_MIN(aSrcRect.width, dstRect.width - aSrcRect.x); PRInt32 height = PR_MIN(aSrcRect.height, dstRect.height - aSrcRect.y); // The clipped image must now fully fit within destination image frame NS_ASSERTION((aSrcRect.x >= 0) && (aSrcRect.y >= 0) && (aSrcRect.x + width <= dstRect.width) && (aSrcRect.y + height <= dstRect.height), "imgContainer::DrawFrameTo: Invalid aSrcRect"); // clipped image size may be smaller than source, but not larger NS_ASSERTION((width <= aSrcRect.width) && (height <= aSrcRect.height), "imgContainer::DrawFrameTo: source must be smaller than dest"); if (NS_FAILED(aDst->LockImageData())) return NS_ERROR_FAILURE; // Get pointers to image data PRUint32 size; PRUint8 *srcPixels; gfx_color *colormap; gfx_color *dstPixels; aSrc->GetImageData(&srcPixels, &size); aDst->GetImageData((PRUint8**)&dstPixels, &size); aSrc->GetPaletteData(&colormap, &size); if (!srcPixels || !dstPixels || !colormap) { aDst->UnlockImageData(); return NS_ERROR_FAILURE; } // Skip to the right offset dstPixels += aSrcRect.x + (aSrcRect.y * dstRect.width); if (format == gfxIFormats::PAL) { for (PRInt32 r = height; r > 0; --r) { for (PRInt32 c = 0; c < width; c++) { dstPixels[c] = colormap[srcPixels[c]]; } // Go to the next row in the source resp. destination image srcPixels += aSrcRect.width; dstPixels += dstRect.width; } } else { // With transparent source, skip transparent pixels for (PRInt32 r = height; r > 0; --r) { for (PRInt32 c = 0; c < width; c++) { const PRUint32 color = colormap[srcPixels[c]]; if (color) dstPixels[c] = color; } // Go to the next row in the source resp. destination image srcPixels += aSrcRect.width; dstPixels += dstRect.width; } } aDst->UnlockImageData(); return NS_OK; } nsCOMPtr<nsIImage> srcImg(do_GetInterface(aSrc)); nsRefPtr<gfxPattern> srcPatt; srcImg->GetPattern(getter_AddRefs(srcPatt)); nsCOMPtr<nsIImage> dstImg(do_GetInterface(aDst)); nsRefPtr<gfxASurface> dstSurf; // Note: dstImage has LockImageData() called on it above, so it's safe to get // the surface. dstImg->GetSurface(getter_AddRefs(dstSurf)); gfxContext dst(dstSurf); dst.Translate(gfxPoint(aSrcRect.x, aSrcRect.y)); dst.Rectangle(gfxRect(0, 0, aSrcRect.width, aSrcRect.height), PR_TRUE); // first clear the surface if the blend flag says so PRInt32 blendMethod; aSrc->GetBlendMethod(&blendMethod); if (blendMethod == imgIContainer::kBlendSource) { gfxContext::GraphicsOperator defaultOperator = dst.CurrentOperator(); dst.SetOperator(gfxContext::OPERATOR_CLEAR); dst.Fill(); dst.SetOperator(defaultOperator); } dst.SetPattern(srcPatt); dst.Paint(); return NS_OK; }