void Objectness::gradientGray(CMat &bgr3u, Mat &mag1u) { Mat g1u; cvtColor(bgr3u, g1u, cv::COLOR_BGR2GRAY); const int H = g1u.rows, W = g1u.cols; Mat Ix(H, W, CV_32S), Iy(H, W, CV_32S); // Left/right most column Ix for (int y = 0; y < H; y++) { Ix.at<int>(y, 0) = abs(g1u.at<byte>(y, 1) - g1u.at<byte>(y, 0)) * 2; Ix.at<int>(y, W-1) = abs(g1u.at<byte>(y, W-1) - g1u.at<byte>(y, W-2)) * 2; } // Top/bottom most column Iy for (int x = 0; x < W; x++) { Iy.at<int>(0, x) = abs(g1u.at<byte>(1, x) - g1u.at<byte>(0, x)) * 2; Iy.at<int>(H-1, x) = abs(g1u.at<byte>(H-1, x) - g1u.at<byte>(H-2, x)) * 2; } // Find the gradient for inner regions for (int y = 0; y < H; y++) for (int x = 1; x < W-1; x++) Ix.at<int>(y, x) = abs(g1u.at<byte>(y, x+1) - g1u.at<byte>(y, x-1)); for (int y = 1; y < H-1; y++) for (int x = 0; x < W; x++) Iy.at<int>(y, x) = abs(g1u.at<byte>(y+1, x) - g1u.at<byte>(y-1, x)); gradientXY(Ix, Iy, mag1u); }
cv::Mat ArmObjSegmentation::getXImageDeriv(cv::Mat& input_img) { cv::Mat Ix(input_img.size(), CV_32FC1); // Get image X derivative cv::filter2D(input_img, Ix, CV_32F, dx_kernel_); return Ix; }
void Objectness::gradientHSV(CMat &bgr3u, Mat &mag1u) { Mat hsv3u; cvtColor(bgr3u, hsv3u, cv::COLOR_BGR2HSV); const int H = hsv3u.rows, W = hsv3u.cols; Mat Ix(H, W, CV_32S), Iy(H, W, CV_32S); // Left/right most column Ix for (int y = 0; y < H; y++) { Ix.at<int>(y, 0) = vecDist3b(hsv3u.at<Vec3b>(y, 1), hsv3u.at<Vec3b>(y, 0)); Ix.at<int>(y, W-1) = vecDist3b(hsv3u.at<Vec3b>(y, W-1), hsv3u.at<Vec3b>(y, W-2)); } // Top/bottom most column Iy for (int x = 0; x < W; x++) { Iy.at<int>(0, x) = vecDist3b(hsv3u.at<Vec3b>(1, x), hsv3u.at<Vec3b>(0, x)); Iy.at<int>(H-1, x) = vecDist3b(hsv3u.at<Vec3b>(H-1, x), hsv3u.at<Vec3b>(H-2, x)); } // Find the gradient for inner regions for (int y = 0; y < H; y++) for (int x = 1; x < W-1; x++) Ix.at<int>(y, x) = vecDist3b(hsv3u.at<Vec3b>(y, x+1), hsv3u.at<Vec3b>(y, x-1))/2; for (int y = 1; y < H-1; y++) for (int x = 0; x < W; x++) Iy.at<int>(y, x) = vecDist3b(hsv3u.at<Vec3b>(y+1, x), hsv3u.at<Vec3b>(y-1, x))/2; gradientXY(Ix, Iy, mag1u); }
void Objectness::gradientRGB(CMat &bgr3u, Mat &mag1u) { const int H = bgr3u.rows, W = bgr3u.cols; Mat Ix(H, W, CV_32S), Iy(H, W, CV_32S); // Left/right most column Ix for (int y = 0; y < H; y++) { Ix.at<int>(y, 0) = bgrMaxDist(bgr3u.at<Vec3b>(y, 1), bgr3u.at<Vec3b>(y, 0))*2; Ix.at<int>(y, W-1) = bgrMaxDist(bgr3u.at<Vec3b>(y, W-1), bgr3u.at<Vec3b>(y, W-2))*2; } // Top/bottom most column Iy for (int x = 0; x < W; x++) { Iy.at<int>(0, x) = bgrMaxDist(bgr3u.at<Vec3b>(1, x), bgr3u.at<Vec3b>(0, x))*2; Iy.at<int>(H-1, x) = bgrMaxDist(bgr3u.at<Vec3b>(H-1, x), bgr3u.at<Vec3b>(H-2, x))*2; } // Find the gradient for inner regions for (int y = 0; y < H; y++) { const Vec3b *dataP = bgr3u.ptr<Vec3b>(y); for (int x = 2; x < W; x++) Ix.at<int>(y, x-1) = bgrMaxDist(dataP[x-2], dataP[x]); // bgr3u.at<Vec3b>(y, x+1), bgr3u.at<Vec3b>(y, x-1)); } for (int y = 1; y < H-1; y++) { const Vec3b *tP = bgr3u.ptr<Vec3b>(y-1); const Vec3b *bP = bgr3u.ptr<Vec3b>(y+1); for (int x = 0; x < W; x++) Iy.at<int>(y, x) = bgrMaxDist(tP[x], bP[x]); } gradientXY(Ix, Iy, mag1u); }
// private CListBoxEBX::LItem* CListBoxEBX::Item(int ix) { int i =Ix(ix); if(i == -1) return NULL; return (LItem *)m_pSuper->GetItemData(i); }
DWORD_PTR CListBoxEBX::GetItemData1(int Item) { int i =Ix(Item); if(i == -1) return 0; LItem *plbi = (LItem*)m_pSuper->GetItemData(i); return plbi->id; }
BOOL CListBoxEBX::RemoveAt(int ix) { int i =Ix(ix); if(i == -1) return FALSE; LItem *plbi = (LItem*)m_pSuper->GetItemData(i); if(m_pSuper->DeleteString(i) == LB_ERR || plbi == NULL) return FALSE; delete plbi; return TRUE; }
void Calc::computeVelocity(const cv::Mat& old, const cv::Mat& current) { // Smooth // cv::GaussianBlur(current, current, cv::Size(3, 3), 0, 0); // cv::dilate(current, current, cv::Mat()); // Moments cv::Mat_<double> It = current - old; cv::Mat_<double> Ix, Iy; cv::Sobel(old, Ix, CV_64F, 1, 0); cv::Sobel(old, Iy, CV_64F, 0, 1); cv::Mat_<double> Ixy = Ix.mul(Iy); cv::Mat_<double> Ixt = Ix.mul(It); cv::Mat_<double> Iyt = Iy.mul(It); Ix = Ix.mul(Ix); Iy = Iy.mul(Iy); // Convolution cv::GaussianBlur(Ix , Ix , _window_size, 0, 0); cv::GaussianBlur(Iy , Iy , _window_size, 0, 0); cv::GaussianBlur(Ixy, Ixy, _window_size, 0, 0); cv::GaussianBlur(Ixt, Ixt, _window_size, 0, 0); cv::GaussianBlur(Iyt, Iyt, _window_size, 0, 0); // Computation for (size_t i = 0; i < _ny; ++i) { for (size_t j = 0; j < _nx; ++j) { const size_t ii = boost::math::round(It.size().height * (i + 0.5) / _ny); const size_t jj = boost::math::round(It.size().width * (j + 0.5) / _nx); cv::Matx22d M; M(0, 0) = Ix(ii, jj); M(0, 1) = Ixy(ii, jj); M(1, 0) = Ixy(ii, jj); M(1, 1) = Iy(ii, jj); cv::Matx21d b, x; b(0) = -Ixt(ii, jj); b(1) = -Iyt(ii, jj); cv::solve(M, b, x); _out.velocity.at<float>(i, j, 0) = _scale * x(0); _out.velocity.at<float>(i, j, 1) = _scale * x(1); } } // Smooth // cv::GaussianBlur(_out.velocity, _out.velocity, cv::Size(3, 3), 0, 0); }
void HarrisDetector::harrisCorners(const cv::Mat& image, std::vector<cv::KeyPoint>& keyPoints, int oct) const { cv::Mat blur_img(image.size(), CV_32F); cv::Mat Ix(image.size(), CV_32F), Iy(image.size(), CV_32F); cv::Mat Ix2(image.size(), CV_32F), Iy2(image.size(), CV_32F), IxIy(image.size(), CV_32F); cv::Mat A(image.size(), CV_32F), B(image.size(), CV_32F), C(image.size(), CV_32F); cv::Mat theta_Ix(image.size(), CV_32F), theta_Iy(image.size(), CV_32F); cv::GaussianBlur(image, blur_img, cv::Size(harris_blur_size,harris_blur_size), harris_blur_variance, harris_blur_variance, cv::BORDER_DEFAULT ); blur_img.convertTo(blur_img, CV_32F); cv::Sobel( blur_img, Ix, CV_32F, 1, 0, 3, 1, 0, cv::BORDER_DEFAULT ); cv::Sobel( blur_img, Iy, CV_32F, 0, 1, 3, 1, 0, cv::BORDER_DEFAULT ); cv::Mat kernel_x, kernel_y, sX, sY; cv::getDerivKernels(sX, sY, 1, 0, harris_window_size, false, CV_32F); kernel_x = sX * sY.t(); cv::getDerivKernels(sX, sY, 0, 1, harris_window_size, false, CV_32F); kernel_y = sX * sY.t(); cv::filter2D(blur_img, theta_Ix, -1, kernel_x, cv::Point(-1,-1), 0, cv::BORDER_DEFAULT); cv::filter2D(blur_img, theta_Iy, -1, kernel_y, cv::Point(-1,-1), 0, cv::BORDER_DEFAULT); Ix2 = Ix.mul(Ix); Iy2 = Iy.mul(Iy); IxIy = Ix.mul(Iy); float sigma = 0.3*((harris_window_size-1)*0.5-1)+0.8; cv::Mat gaussKernel = cv::getGaussianKernel(harris_window_size, sigma, CV_32F); cv::filter2D(Ix2, A, -1, gaussKernel, cv::Point(-1,-1), 0, cv::BORDER_DEFAULT ); cv::filter2D(IxIy, B, -1, gaussKernel, cv::Point(-1,-1), 0, cv::BORDER_DEFAULT ); cv::filter2D(Iy2, C, -1, gaussKernel, cv::Point(-1,-1), 0, cv::BORDER_DEFAULT ); cv::Mat H(image.size(), CV_32F); H = A.mul(C) - B.mul(B) - 0.04*(A+C).mul(A+C); for(int i=0;i<H.rows;i++) { for(int j=0;j<H.cols;j++) { float& Hij = H.at<float>(i,j); if(Hij>0) { float theta = std::atan2(theta_Iy.at<float>(i,j), theta_Ix.at<float>(i,j)); keyPoints.push_back(cv::KeyPoint(cv::Point(j,i),0,theta,Hij,oct)); } } } }
void OpticalFlow::baseCalculate(cv::Mat& Im1, cv::Mat& Im2, flowUV& UV, const OpticalFlowParams& params){ int rows = Im1.rows; int cols = Im1.cols; FlowOperator flowOp(rows, cols); FArray X0(2 * rows * cols, false); FArray dUdV(2 * rows * cols, true, 0); cv::Mat Ix1(rows, cols, OPTFLOW_TYPE); cv::Mat Iy1(rows, cols, OPTFLOW_TYPE); cv::Mat Ix(rows, cols, OPTFLOW_TYPE); cv::Mat Iy(rows, cols, OPTFLOW_TYPE); getDXsCV(Im1, Ix1, Iy1); for (int i = 0; i < params.getIters(); ++i){ cv::Mat Ix2(rows, cols, OPTFLOW_TYPE); cv::Mat Iy2(rows, cols, OPTFLOW_TYPE); cv::Mat It(rows, cols, OPTFLOW_TYPE); cv::Mat im2Warpped(rows, cols, Im1.type()); WarpImage(Im2, UV.getU(), UV.getV(), im2Warpped); getDXsCV(im2Warpped, Ix2, Iy2); Ix = params.getWeightedDeriveFactor() * (Ix1 + Ix2); Iy = params.getWeightedDeriveFactor() * (Iy1 + Iy2); cv::subtract(im2Warpped, Im1, It); if (params.getDisplayDerivativs()){ cv::imshow("Derivative Ix", Ix); cv::imshow("Derivative Iy", Iy); cv::waitKey(1); } cv::Mat Du(rows, cols, OPTFLOW_TYPE, cv::Scalar(0)); cv::Mat Dv(rows, cols, OPTFLOW_TYPE, cv::Scalar(0)); for (int j = 0; j < params.getLinearIters(); ++j){ #if OPTFLOW_VERBOSE cout << "solving Ax=b with SOR "; clock_t start = std::clock(); #endif flowOp.construct(UV, Du, Dv, Ix, Iy, It, params); memcpy(X0.ptr, UV.getU().data, rows * cols * sizeof(float)); memcpy(X0.ptr + (rows * cols), UV.getV().data, rows * cols * sizeof(float)); //UtilsDebug::printCRSSparseMat(flowOp.getA(), "aaaa.txt"); if (params.getCheckResidualTolerance()){ LinearSolver::sparseMatSor(flowOp.getA(), X0 ,dUdV, flowOp.getb(), params.getOverRelaxation(), params.getSorIters(), params.getResidualTolerance()); }else{ //LinearSolver::multigrid(10,10,flowOp.getA(),flowOp.getb(), params.getResidualTolerance(), dUdV, 20, 20, LinearSolver::vCycle); LinearSolver::sparseMatSorNoResidual(flowOp.getA(), X0 ,dUdV, flowOp.getb(), params.getOverRelaxation(), params.getSorIters()); } #if OPTFLOW_VERBOSE std::cout<<" --- "<< (std::clock() - start) / (double)CLOCKS_PER_SEC <<'\n'; #endif #if OPTFLOW_DEBUG for(int i = 0; i < dUdV.size(); ++i){ if (!(dUdV.ptr[i] == dUdV.ptr[i])){ cout << "ERROR - NAN"; } } #endif UtilsMat::clamp(dUdV, -1, 1); memcpy(Du.data, dUdV.ptr, rows * cols * sizeof(float)); memcpy(Dv.data, dUdV.ptr + (rows * cols), rows * cols * sizeof(float)); flowUV UV0(UV); UV.getU() += Du; UV.getV() += Dv; cv::Mat tmpU, tmpV; UV.getU().copyTo(tmpU); UV.getV().copyTo(tmpV); WeightedMedianFilter::computeMedianFilter(UV.getU(), UV.getV(), Im1, Im2, params.getMedianFilterRadius()); Du = UV.getU() - UV0.getU(); Dv = UV.getV() - UV0.getV(); UV0.getU().copyTo(UV.getU()); UV0.getV().copyTo(UV.getV()); UV0.getU() += Du; UV0.getV() += Dv; if (params.isDisplay()) UtilsFlow::DrawFlow(UV0.getU(), UV0.getV(), "Flow"); } UV.getU() += Du; UV.getV() += Dv; } }
// Loop through the image to compute the harris corner values as described in class // srcImage: grayscale of original image // harrisImage: populate the harris values per pixel in this image void computeHarrisValues(CFloatImage &srcImage, CFloatImage &harrisImage, CFloatImage &orientationImage) { int w = srcImage.Shape().width; // image width int h = srcImage.Shape().height; // image height // Create images to store x-derivative and y-derivative values CFloatImage Ix(w,h,1); CFloatImage Iy(w,h,1); CFloatImage Ix_blur(w,h,1); CFloatImage Iy_blur(w,h,1); // Compute x-derivative values by convolving image with x sobel filter Convolve(srcImage, Ix, ConvolveKernel_SobelX); // Compute y-derivative values by convolving image with y sobel filter Convolve(srcImage, Iy, ConvolveKernel_SobelY); // Apply a 7x7 gaussian blur to the grayscale image CFloatImage blurImage(w,h,1); Convolve(srcImage, blurImage, ConvolveKernel_7x7); // Compute x-derivative values by convolving blurred image with x sobel filter Convolve(blurImage, Ix_blur, ConvolveKernel_SobelX); // Compute y-derivative values by convolving blurred image with y sobel filter Convolve(blurImage, Iy_blur, ConvolveKernel_SobelY); // Declare additional variables int newX, newY; // (x,y) coordinate for pixel in 5x5 sliding window float dx, dy; // x-derivative, y-derivative values double HMatrix[4]; // Harris matrix double determinant; // determinant of Harris matrix double trace; // trace of Harris matrix int padType = 2; // select variable for what type of padding to use: , 0->zero, 1->edge, 2->reflect // Loop through 'srcImage' and compute harris score for each pixel for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { // reset Harris matrix values to 0 memset(HMatrix, 0, sizeof(HMatrix)); // Loop through pixels in 5x5 window to calculate Harris matrix for (int j = 0; j < 25; j++) { find5x5Index(x,y,j,&newX,&newY); if(srcImage.Shape().InBounds(newX, newY)) { dx = Ix.Pixel(newX,newY,0); dy = Iy.Pixel(newX,newY,0); } else { // Depending on value of padType, perform different types of border padding switch (padType) { case 1: // 1 -> replicate border values if (newX < 0) { newX = 0; } else if (newX >= w) { newX = w-1; } if (newY < 0) { newY = 0; } else if (newY >= h) { newY = h-1; } dx = Ix.Pixel(newX,newY,0); dy = Iy.Pixel(newX,newY,0); break; case 2: // 2 -> reflect border pixels if (newX < 0) { newX = -newX; } else if (newX >= w) { newX = w-(newX%w)-1; } if (newY < 0) { newY = -newY; } else if (newY >= h) { newY = h-(newY%h)-1; } dx = Ix.Pixel(newX,newY,0); dy = Iy.Pixel(newX,newY,0); break; default: // 0 -> zero padding dx = 0.0; dy = 0.0; break; } } HMatrix[0] += dx*dx*gaussian5x5[j]; HMatrix[1] += dx*dy*gaussian5x5[j]; HMatrix[2] += dx*dy*gaussian5x5[j]; HMatrix[3] += dy*dy*gaussian5x5[j]; } // Calculate determinant and trace of harris matrix determinant = (HMatrix[0] * HMatrix[3]) - (HMatrix[1] * HMatrix[2]); trace = HMatrix[0] + HMatrix[3]; // Compute corner strength function c(H) = determinant(H)/trace(H) // and save result into harrisImage if(trace == 0) harrisImage.Pixel(x,y,0) = 0.0; else harrisImage.Pixel(x,y,0) = (determinant / trace); // Compute orientation and save result in 'orientationImage' dx = Ix_blur.Pixel(x,y,0); dy = Iy_blur.Pixel(x,y,0); if(dx == 0.0 && dy == 0.0) orientationImage.Pixel(x,y,0) = 0.0; else orientationImage.Pixel(x,y,0) = atan2(dy, dx); } } }