static void gaussianBlur(const T * __srcp, float * temp, float * dstp, const float * weightsH, const float * weightsV, const int width, const int height, const int srcStride, const int dstStride, const int radiusH, const int radiusV, const float offset) noexcept { const int diameter = radiusV * 2 + 1; const T ** _srcp = new const T *[diameter]; _srcp[radiusV] = __srcp; for (int i = 1; i <= radiusV; i++) { _srcp[radiusV - i] = _srcp[radiusV - 1 + i]; _srcp[radiusV + i] = _srcp[radiusV] + srcStride * i; } weightsH += radiusH; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x += 4) { Vec4f sum = zero_4f(); for (int i = 0; i < diameter; i++) { if (std::is_same<T, uint8_t>::value) { const Vec4f srcp = to_float(Vec4i().load_4uc(_srcp[i] + x)); sum = mul_add(srcp, weightsV[i], sum); } else if (std::is_same<T, uint16_t>::value) { const Vec4f srcp = to_float(Vec4i().load_4us(_srcp[i] + x)); sum = mul_add(srcp, weightsV[i], sum); } else { const Vec4f srcp = Vec4f().load_a(_srcp[i] + x); sum = mul_add(srcp + offset, weightsV[i], sum); } } sum.store_a(temp + x); } for (int i = 1; i <= radiusH; i++) { temp[-i] = temp[-1 + i]; temp[width - 1 + i] = temp[width - i]; } for (int x = 0; x < width; x += 4) { Vec4f sum = zero_4f(); for (int i = -radiusH; i <= radiusH; i++) { const Vec4f srcp = Vec4f().load(temp + x + i); sum = mul_add(srcp, weightsH[i], sum); } sum.stream(dstp + x); } for (int i = 0; i < diameter - 1; i++) _srcp[i] = _srcp[i + 1]; if (y < height - 1 - radiusV) _srcp[diameter - 1] += srcStride; else if (y > height - 1 - radiusV) _srcp[diameter - 1] -= srcStride; dstp += dstStride; } delete[] _srcp; }
std::vector< Vec4i > get_v_lines(Mat& img) { int h = img.rows; int w = img.cols; std::vector< Vec4i > lines; unsigned int length_th = 200; for (int y = 0; y<h; y++) { int x1 = 0; int x2 = 0; int in_row = 0; Vec4i longest_line(0, 0, 0, 0); for (int x = 0; x<w; x++) { if (img.at<unsigned char>(y, x) < 140) { if (in_row == 0) x1 = x; in_row += 1; } else { if (in_row > length_th) lines.push_back(Vec4i(x1, y, x, y)); in_row = 0; } } if (in_row > length_th) lines.push_back(Vec4i(x1, y, w - 1, y)); } return lines; }
static void gaussianBlurH(const T * _srcp, float * temp, float * dstp, const float * weights, const int width, const int height, const int srcStride, const int dstStride, const int radius, const float offset) noexcept { weights += radius; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x += 4) { if (std::is_same<T, uint8_t>::value) to_float(Vec4i().load_4uc(_srcp + x)).store_a(temp + x); else if (std::is_same<T, uint16_t>::value) to_float(Vec4i().load_4us(_srcp + x)).store_a(temp + x); else (Vec4f().load_a(_srcp + x) + offset).store_a(temp + x); } for (int i = 1; i <= radius; i++) { temp[-i] = temp[-1 + i]; temp[width - 1 + i] = temp[width - i]; } for (int x = 0; x < width; x += 4) { Vec4f sum = zero_4f(); for (int i = -radius; i <= radius; i++) { const Vec4f srcp = Vec4f().load(temp + x + i); sum = mul_add(srcp, weights[i], sum); } sum.stream(dstp + x); } _srcp += srcStride; dstp += dstStride; } }
static void copyPlane(const T * srcp, float * dstp, const int width, const int height, const int srcStride, const int dstStride, const float offset) noexcept { for (int y = 0; y < height; y++) { for (int x = 0; x < width; x += 4) { if (std::is_same<T, uint8_t>::value) to_float(Vec4i().load_4uc(srcp + x)).stream(dstp + x); else if (std::is_same<T, uint16_t>::value) to_float(Vec4i().load_4us(srcp + x)).stream(dstp + x); else (Vec4f().load_a(srcp + x) + offset).stream(dstp + x); } srcp += srcStride; dstp += dstStride; } }
ResultLines lineDetection(Mat origin,int w1, int w2) { vector<Vec2f> lines; ResultLines resultlines; Mat canny; Mat test = origin.clone(); Canny(origin,canny,100,500); HoughLines(canny, lines, 1, CV_PI / 180, w1, w2, 0); setLineSize(lines.size()); for (size_t i = 0; i < lines.size(); i++) { float rho = lines[i][0], theta = lines[i][1]; Point pt1, pt2; double a = cos(theta), b = sin(theta); double x0 = a * rho, y0 = b * rho; pt1.x = cvRound(x0 + 1000 * (-b)); pt1.y = cvRound(y0 + 1000 * (a)); pt2.x = cvRound(x0 - 1000 * (-b)); pt2.y = cvRound(y0 - 1000 * (a)); Vec4i vec4i = Vec4i(pt1.x,pt1.y,pt2.x,pt2.y); double degree = theta/CV_PI*180; line(test,pt1,pt2,Scalar(255,255,255),3); degreeChecking(degree,vec4i,pt1,pt2,&resultlines,origin); resultlines.allLines.push_back(vec4i); } return resultlines; }
// return the vertex of the edge crossing (create it if necessary) between given grid points and function values int MarchingTriangles:: find_edge_cross(const Vec2i& x0, const Vec2i& x1, float p0, float p1) { unsigned int vertex_index; if(edge_cross.get_entry(Vec4i(x0.v[0], x0.v[1], x1.v[0], x1.v[1]), vertex_index)){ return vertex_index; }else if(edge_cross.get_entry(Vec4i(x1.v[0], x1.v[1], x0.v[0], x0.v[1]), vertex_index)){ return vertex_index; }else{ float a=p1/(p1-p0), b=1-a; vertex_index=(int)x.size(); x.push_back(Vec2f(origin[0]+dx*(a*x0[0]+b*x1[0]), origin[1]+dx*(a*x0[1]+b*x1[1]))); edge_cross.add(Vec4i(x0.v[0], x0.v[1], x1.v[0], x1.v[1]), vertex_index); return vertex_index; } }
Vec4i GLBoxSelector::getBox() const { return Vec4i ( box.v1*viewport.v3, box.v2*viewport.v4, (box.v3-box.v1) * viewport.v3, (box.v4-box.v2) * viewport.v4 ); }
internal rc_draw_rect_multitextured* PushDrawRectMultitextured(render_buffer* RenderBuffer, vec2 Mid, vec2 Size) { rc_draw_rect_multitextured Command = {0}; Command.Mid = Mid; Command.Size = Size; Command.Color = Vec4i(1, 1, 1, 1); return (rc_draw_rect_multitextured*)PushCommand(RenderBuffer, RenderCommand_DrawRectMultitextured, &Command); }
XCamReturn DnnObjectDetection::get_bounding_boxes (const float* result_ptr, const uint32_t idx, std::vector<Vec4i> &boxes, std::vector<int32_t> &classes) { if (!_model_created) { XCAM_LOG_ERROR ("Please create the model firstly!"); return XCAM_RETURN_ERROR_ORDER; } if (!result_ptr) { XCAM_LOG_ERROR ("Inference results error!"); return XCAM_RETURN_ERROR_PARAM; } DnnInferInputOutputInfo output_infos; get_model_output_info (output_infos); uint32_t image_width = get_input_image_width (idx); uint32_t image_height = get_input_image_height (idx); uint32_t max_proposal_count = output_infos.channels[idx]; uint32_t object_size = output_infos.object_size[idx]; uint32_t box_count = 0; for (uint32_t cur_proposal = 0; cur_proposal < max_proposal_count; cur_proposal++) { float image_id = result_ptr[cur_proposal * object_size + 0]; if (image_id < 0) { break; } float label = result_ptr[cur_proposal * object_size + 1]; float confidence = result_ptr[cur_proposal * object_size + 2]; float xmin = result_ptr[cur_proposal * object_size + 3] * image_width; float ymin = result_ptr[cur_proposal * object_size + 4] * image_height; float xmax = result_ptr[cur_proposal * object_size + 5] * image_width; float ymax = result_ptr[cur_proposal * object_size + 6] * image_height; if (confidence > 0.5) { classes.push_back(static_cast<int32_t>(label)); boxes.push_back (Vec4i ( static_cast<int32_t>(xmin), static_cast<int32_t>(ymin), static_cast<int32_t>(xmax - xmin), static_cast<int32_t>(ymax - ymin) )); XCAM_LOG_DEBUG ("Proposal:%d label:%d confidence:%f", cur_proposal, classes[box_count], confidence); XCAM_LOG_DEBUG ("Boxes[%d] {%d, %d, %d, %d}", box_count, boxes[box_count][0], boxes[box_count][1], boxes[box_count][2], boxes[box_count][3]); box_count++; } } return XCAM_RETURN_NO_ERROR; }
std::vector< Vec4i > get_h_lines(Mat& img) { int h = img.rows; int w = img.cols; std::vector< Vec4i > lines; int length_th = 100; for (int x = 0; x<w; x++) { int y1 = 0; int in_row = 0; int longest = 0; Vec4i longest_line(0, 0, 0, 0); for (int y = 0; y<h; y++) { if (img.at<unsigned char>(y, x) < 140) { if (in_row == 0) y1 = y; in_row += 1; } else { if (in_row > length_th) lines.push_back(Vec4i(x, y1, x, y - 1)); in_row = 0; } } if (in_row > length_th) lines.push_back(Vec4i(x, y1, x, h - 1)); } return lines; }
internal rc_draw_rect* PushDrawRect(render_buffer* RenderBuffer, vec2 Mid, vec2 Size, texture_slot* TextureSlot) { rc_draw_rect Command = {0}; Command.Mid = Mid; Command.Size = Size; Command.Color = Vec4i(1, 1, 1, 1); Command.TextureSlot = TextureSlot; Command.MinUV = Vec2i(0, 0); Command.MaxUV = Vec2i(1, 1); return (rc_draw_rect*)PushCommand(RenderBuffer, RenderCommand_DrawRect, &Command); }
Grid3D<uint32_t> computeDistanceMap26_WithQueue(const GridType& grid, Predicate isInObject, bool outsideIsObject) { auto w = grid.width(), h = grid.height(), d = grid.depth(); uint32_t maxDist = std::numeric_limits<uint32_t>::max(); Grid3D<uint32_t> distanceGrid(w, h, d, maxDist); std::queue<Vec4i> queue; for(auto z = 0u; z < d; ++z) { for(auto y = 0u; y < h; ++y) { for(auto x = 0u; x < w; ++x) { auto voxel = Vec3i(x, y, z); if(!isInObject(x, y, z, grid)) { queue.push(Vec4i(voxel, 0u)); } else if(!outsideIsObject && (x == 0 || y == 0 || z == 0 || x == w - 1 || y == h - 1 || z == d - 1)) { queue.push(Vec4i(voxel, 1u)); } } } } while(!queue.empty()) { auto voxel = Vec3i(queue.front()); auto dist = queue.front().w; queue.pop(); if(dist < distanceGrid(voxel)) { distanceGrid(voxel) = dist; foreach26Neighbour(grid.resolution(), voxel, [&](const Vec3i& neighbour) { auto newDist = dist + 1; if(newDist < distanceGrid(neighbour)) { queue.push(Vec4i(neighbour, newDist)); } }); } } return distanceGrid; }
void Objectness::getRandomBoxes(vector<vector<Vec4i> > &boxesTests, int num) { const int TestNum = _voc.testSet.size(); boxesTests.resize(TestNum); #pragma omp parallel for for (int i = 0; i < TestNum; i++) { Mat imgs3u = imread(format(_S(_voc.imgPathW), _S(_voc.testSet[i]))); int H = imgs3u.cols, W = imgs3u.rows; boxesTests[i].reserve(num); for (int k = 0; k < num; k++) { int x1 = rand()%W + 1, x2 = rand()%W + 1; int y1 = rand()%H + 1, y2 = rand()%H + 1; boxesTests[i].push_back(Vec4i(min(x1, x2), min(y1, y2), max(x1, x2), max(y1, y2))); } } evaluatePerImgRecall(boxesTests, "PerImgAll.m", num); }
void DataSetVOC::loadBox(const FileNode &fn, vector<Vec4i> &boxes, vecI &clsIdx){ string isDifficult; fn["difficult"]>>isDifficult; if (isDifficult == "1") return; string strXmin, strYmin, strXmax, strYmax; fn["bndbox"]["xmin"] >> strXmin; fn["bndbox"]["ymin"] >> strYmin; fn["bndbox"]["xmax"] >> strXmax; fn["bndbox"]["ymax"] >> strYmax; boxes.push_back(Vec4i(atoi(_S(strXmin)), atoi(_S(strYmin)), atoi(_S(strXmax)), atoi(_S(strYmax)))); string clsName; fn["name"]>>clsName; clsIdx.push_back(findFromList(clsName, classNames)); CV_Assert_(clsIdx[clsIdx.size() - 1] >= 0, ("Invalidate class name\n")); }
arrayVec3f voxelSurfaceContruction::getQuadFace(Vec3f leftDown, Vec3f rightUp, direction d) { arrayVec3f pts; Vec3f U = rightUp, o = leftDown; arrayVec3f _points; _points.push_back(Vec3fL(o, o, o)); _points.push_back(Vec3fL(o, U, o)); _points.push_back(Vec3fL(U, U, o)); _points.push_back(Vec3fL(U, o, o)); _points.push_back(Vec3fL(o, o, U)); _points.push_back(Vec3fL(o, U, U)); _points.push_back(Vec3fL(U, U, U)); _points.push_back(Vec3fL(U, o, U)); Vec4i faces; switch (d) { case voxelSurfaceContruction::X_MINUS: faces = Vec4i(0, 4, 5, 1); break; case voxelSurfaceContruction::X_PLUS: faces = Vec4i(2, 6, 7, 3); break; case voxelSurfaceContruction::Y_MINUS: faces = Vec4i(0, 3, 7, 4); break; case voxelSurfaceContruction::Y_PLUS: faces = Vec4i(1, 5, 6, 2); break; case voxelSurfaceContruction::Z_MINUS: faces = Vec4i(0, 1, 2, 3); break; case voxelSurfaceContruction::Z_PLUS: faces = Vec4i(4, 7, 6, 5); break; default: ASSERT(0); break; } for (int i = 0; i < 4; i++) { pts.push_back(_points[faces[i]]); } return pts; }
Vec4i getMaskRange(CMat &mask1u, int ext = 0) { int maxX = INT_MIN, maxY = INT_MIN, minX = INT_MAX, minY = INT_MAX, rows = mask1u.rows, cols = mask1u.cols; for (int r = 0; r < rows; r++) { const byte* data = mask1u.ptr<byte>(r); for (int c = 0; c < cols; c++) if (data[c] > 10) { maxX = max(maxX, c); minX = min(minX, c); maxY = max(maxY, r); minY = min(minY, r); } } maxX = maxX + ext + 1 < cols ? maxX + ext + 1 : cols; maxY = maxY + ext + 1 < rows ? maxY + ext + 1 : rows; minX = minX - ext > 0 ? minX - ext : 0; minY = minY - ext > 0 ? minY - ext : 0; return Vec4i(minX + 1, minY + 1, maxX, maxY); // Rect(minX, minY, maxX - minX, maxY - minY); }
void Renderer::ProcessThread( std::shared_ptr<Thread_InitParam> param ) { Task task; auto shared = Create_Thread_SharedData(); shared->rng = std::make_shared<Random>((unsigned long)std::time(nullptr) + param->id); shared->color.assign(commonConfig->width * commonConfig->height, Vec3d()); threadSharedData.push_back(shared); InitializeThread(param, shared); while (!queue.Done()) { queue.Dequeue(task); if (queue.Done()) { break; } switch (task.command) { case Command::Render: { ProcessThread_Render(shared); break; } case Command::UpdateImage: { image->Accumulate(Vec4i(0, 0, commonConfig->width, commonConfig->height), shared->color); shared->color.assign(commonConfig->width * commonConfig->height, Vec3d()); break; } } // Barrier if (task.needSync) { std::unique_lock<std::mutex> lock(taskSyncMutex); if (++waitingThreads == commonConfig->numThreads) { waitingThreads = 0; taskSync.notify_all(); } else { taskSync.wait(lock); } } { std::unique_lock<std::mutex> lock(taskFinishedMutex); finishedTasks++; taskFinished.notify_one(); } } }
Vec3i(0,2,1), Vec3i(2,2,0), Vec3i(4,2,1), Vec3i(1,4,4), Vec3i(3,4,4), Vec3i(5,4,4), Vec3i(0,4,2), Vec3i(2,5,2), Vec3i(4,4,2), Vec3i(1,4,0), Vec3i(3,4,0), Vec3i(5,4,0), }; const int num_lattice_tets=46; const Vec4i lattice_tet[46]={ Vec4i(12,4,0,3), Vec4i(0,12,9,10), Vec4i(4,12,0,10), Vec4i(1,4,0,10), Vec4i(13,12,4,10), Vec4i(14,1,2,11), Vec4i(1,14,4,10), Vec4i(14,1,4,5), Vec4i(14,13,4,10), Vec4i(1,14,2,5), Vec4i(1,11,14,10), Vec4i(6,7,16,4), Vec4i(6,4,15,3), Vec4i(15,4,12,3),
// Find all tets with vertices where a corner has vphi>0 and trim them back. static void trim_spikes(TetMesh& mesh, std::vector<float> &vphi) { // keep track of edges we cut - store the index of the new vertex std::map<Vec2i,int> cut_map; // we have a separate list for the results of trimming tets std::vector<Vec4i> new_tets; // go through the existing tets to see what we have to trim for (int t=0; t<(int)mesh.tSize(); ++t) { int p, q, r, s; assign(mesh.T(t), p, q, r, s); if (vphi[p]<=0 && vphi[q]<=0 && vphi[r]<=0 && vphi[s]<=0) continue; // this tet doesn't stick out // We have a plethora of cases to deal with. Let's sort the vertices // by their phi values and break ties with index, remembering if we // have switched orientation or not, to cut down on the number of // cases to handle. Use a sorting network to do the job. bool flipped=false; if (vphi[p]<vphi[q] || (vphi[p]==vphi[q] && p<q)) { std::swap(p, q); flipped = !flipped; } if (vphi[r]<vphi[s] || (vphi[r]==vphi[s] && r<s)) { std::swap(r, s); flipped = !flipped; } if (vphi[p]<vphi[r] || (vphi[p]==vphi[r] && p<r)) { std::swap(p, r); flipped = !flipped; } if (vphi[q]<vphi[s] || (vphi[q]==vphi[s] && q<s)) { std::swap(q, s); flipped = !flipped; } if (vphi[q]<vphi[r] || (vphi[q]==vphi[r] && q<r)) { std::swap(q, r); flipped = !flipped; } // sanity checks assert(vphi[p]>=vphi[q] && vphi[q]>=vphi[r] && vphi[r]>=vphi[s]); //sort assert(vphi[p]>0); // we already skipped interior tets assert(vphi[s]<=0); // we never generate tets with all positive phi // now do the actual trimming if (vphi[s]==0) { // +++0 entirely outside mesh.T(t)=mesh.tets().back(); // overwrite with last tet mesh.tets().pop_back(); // get rid of the last one --t; // decrement to cancel the next for-loop increment } else if (vphi[r]>0) { // +++- just one vertex inside, three out // replace this tet with one clipped back to the isosurface int ps = cut_edge(p, s, mesh, vphi, cut_map), qs = cut_edge(q, s, mesh, vphi, cut_map), rs = cut_edge(r, s, mesh, vphi, cut_map); if (flipped) mesh.T(t) = Vec4i(qs, ps, rs, s); else mesh.T(t) = Vec4i(ps, qs, rs, s); } else if(vphi[q]<0) { // +--- just one vertex outside, three in // Have to tetrahedralize the resulting triangular prism. // Note that the quad faces have to be split in a way that will // be consistent with face-adjacent tets: our sort of pqrs with // consistently broken ties makes this work. We cut the quad to the // deepest vertex (phi as negative as possible). int pq = cut_edge(p, q, mesh, vphi, cut_map), pr = cut_edge(p, r, mesh, vphi, cut_map), ps = cut_edge(p, s, mesh, vphi, cut_map); if (flipped) { mesh.T(t) = Vec4i(q, pq, r, s); new_tets.push_back(Vec4i(r, pq, pr, s)); new_tets.push_back(Vec4i(s, pq, pr, ps)); } else { mesh.T(t)=Vec4i(pq, q, r, s); new_tets.push_back(Vec4i(pq, r, pr, s)); new_tets.push_back(Vec4i(pq, s, pr, ps)); } } else if (vphi[q]>0 && vphi[r]<0) { // ++-- two vertices out, two in int pr = cut_edge(p, r, mesh, vphi, cut_map), ps = cut_edge(p, s, mesh, vphi, cut_map), qr = cut_edge(q, r, mesh, vphi, cut_map), qs = cut_edge(q, s, mesh, vphi, cut_map); if (flipped) { mesh.T(t) = Vec4i(qr, pr, r, s); new_tets.push_back(Vec4i(qs, pr, qr, s)); new_tets.push_back(Vec4i(ps, pr, qs, s)); } else { mesh.T(t) = Vec4i(pr, qr, r, s); new_tets.push_back(Vec4i(pr, qs, qr, s)); new_tets.push_back(Vec4i(pr, ps, qs, s)); } } else if (vphi[q]==0 && vphi[r]==0) { // +00- 1 out, 1 in, 2 surface int ps = cut_edge(p, s, mesh, vphi, cut_map); if (flipped) mesh.T(t) = Vec4i(q, ps, r, s); else mesh.T(t) = Vec4i(ps, q, r, s); } else if (vphi[q]==0) { // +0-- 1 out, 2 in, 1 surface int pr = cut_edge(p, r, mesh, vphi, cut_map), ps = cut_edge(p, s, mesh, vphi, cut_map); if (flipped) { mesh.T(t) = Vec4i(q, pr, r, s); new_tets.push_back(Vec4i(q, ps, pr, s)); } else { mesh.T(t) = Vec4i(pr, q, r, s); new_tets.push_back(Vec4i(ps, q, pr, s)); } } else { // ++0- two out, one in, one surface int ps = cut_edge(p, s, mesh, vphi, cut_map), qs = cut_edge(q, s, mesh, vphi, cut_map); if (flipped) mesh.T(t) = Vec4i(qs, ps, r, s); else mesh.T(t) = Vec4i(ps, qs, r, s); } } // append all the remaining new tets for (size_t t=0; t<new_tets.size(); ++t) mesh.tets().push_back(new_tets[t]); }
void convexityDefects( InputArray _points, InputArray _hull, OutputArray _defects ) { CV_INSTRUMENT_REGION() Mat points = _points.getMat(); int i, j = 0, npoints = points.checkVector(2, CV_32S); CV_Assert( npoints >= 0 ); if( npoints <= 3 ) { _defects.release(); return; } Mat hull = _hull.getMat(); int hpoints = hull.checkVector(1, CV_32S); CV_Assert( hpoints > 0 ); const Point* ptr = points.ptr<Point>(); const int* hptr = hull.ptr<int>(); std::vector<Vec4i> defects; if ( hpoints < 3 ) //if hull consists of one or two points, contour is always convex { _defects.release(); return; } // 1. recognize co-orientation of the contour and its hull bool rev_orientation = ((hptr[1] > hptr[0]) + (hptr[2] > hptr[1]) + (hptr[0] > hptr[2])) != 2; // 2. cycle through points and hull, compute defects int hcurr = hptr[rev_orientation ? 0 : hpoints-1]; CV_Assert( 0 <= hcurr && hcurr < npoints ); for( i = 0; i < hpoints; i++ ) { int hnext = hptr[rev_orientation ? hpoints - i - 1 : i]; CV_Assert( 0 <= hnext && hnext < npoints ); Point pt0 = ptr[hcurr], pt1 = ptr[hnext]; double dx0 = pt1.x - pt0.x; double dy0 = pt1.y - pt0.y; double scale = dx0 == 0 && dy0 == 0 ? 0. : 1./std::sqrt(dx0*dx0 + dy0*dy0); int defect_deepest_point = -1; double defect_depth = 0; bool is_defect = false; j=hcurr; for(;;) { // go through points to achieve next hull point j++; j &= j >= npoints ? 0 : -1; if( j == hnext ) break; // compute distance from current point to hull edge double dx = ptr[j].x - pt0.x; double dy = ptr[j].y - pt0.y; double dist = fabs(-dy0*dx + dx0*dy) * scale; if( dist > defect_depth ) { defect_depth = dist; defect_deepest_point = j; is_defect = true; } } if( is_defect ) { int idepth = cvRound(defect_depth*256); defects.push_back(Vec4i(hcurr, hnext, defect_deepest_point, idepth)); } hcurr = hnext; } Mat(defects).copyTo(_defects); }
void meanShiftSegmentation(const oclMat &src, Mat &dst, int sp, int sr, int minsize, TermCriteria criteria) { CV_Assert(src.type() == CV_8UC4); const int nrows = src.rows; const int ncols = src.cols; const int hr = sr; const int hsp = sp; // Perform mean shift procedure and obtain region and spatial maps oclMat h_rmap, h_spmap; meanShiftProc(src, h_rmap, h_spmap, sp, sr, criteria); Mat rmap = h_rmap; Mat spmap = h_spmap; Graph<SegmLinkVal> g(nrows * ncols, 4 * (nrows - 1) * (ncols - 1) + (nrows - 1) + (ncols - 1)); // Make region adjacent graph from image Vec4b r1; Vec4b r2[4]; Vec2s sp1; Vec2s sp2[4]; int dr[4]; int dsp[4]; for (int y = 0; y < nrows - 1; ++y) { Vec4b *ry = rmap.ptr<Vec4b>(y); Vec4b *ryp = rmap.ptr<Vec4b>(y + 1); Vec2s *spy = spmap.ptr<Vec2s>(y); Vec2s *spyp = spmap.ptr<Vec2s>(y + 1); for (int x = 0; x < ncols - 1; ++x) { r1 = ry[x]; sp1 = spy[x]; r2[0] = ry[x + 1]; r2[1] = ryp[x]; r2[2] = ryp[x + 1]; r2[3] = ryp[x]; sp2[0] = spy[x + 1]; sp2[1] = spyp[x]; sp2[2] = spyp[x + 1]; sp2[3] = spyp[x]; dr[0] = dist2(r1, r2[0]); dr[1] = dist2(r1, r2[1]); dr[2] = dist2(r1, r2[2]); dsp[0] = dist2(sp1, sp2[0]); dsp[1] = dist2(sp1, sp2[1]); dsp[2] = dist2(sp1, sp2[2]); r1 = ry[x + 1]; sp1 = spy[x + 1]; dr[3] = dist2(r1, r2[3]); dsp[3] = dist2(sp1, sp2[3]); g.addEdge(pix(y, x, ncols), pix(y, x + 1, ncols), SegmLinkVal(dr[0], dsp[0])); g.addEdge(pix(y, x, ncols), pix(y + 1, x, ncols), SegmLinkVal(dr[1], dsp[1])); g.addEdge(pix(y, x, ncols), pix(y + 1, x + 1, ncols), SegmLinkVal(dr[2], dsp[2])); g.addEdge(pix(y, x + 1, ncols), pix(y + 1, x, ncols), SegmLinkVal(dr[3], dsp[3])); } } for (int y = 0; y < nrows - 1; ++y) { r1 = rmap.at<Vec4b>(y, ncols - 1); r2[0] = rmap.at<Vec4b>(y + 1, ncols - 1); sp1 = spmap.at<Vec2s>(y, ncols - 1); sp2[0] = spmap.at<Vec2s>(y + 1, ncols - 1); dr[0] = dist2(r1, r2[0]); dsp[0] = dist2(sp1, sp2[0]); g.addEdge(pix(y, ncols - 1, ncols), pix(y + 1, ncols - 1, ncols), SegmLinkVal(dr[0], dsp[0])); } for (int x = 0; x < ncols - 1; ++x) { r1 = rmap.at<Vec4b>(nrows - 1, x); r2[0] = rmap.at<Vec4b>(nrows - 1, x + 1); sp1 = spmap.at<Vec2s>(nrows - 1, x); sp2[0] = spmap.at<Vec2s>(nrows - 1, x + 1); dr[0] = dist2(r1, r2[0]); dsp[0] = dist2(sp1, sp2[0]); g.addEdge(pix(nrows - 1, x, ncols), pix(nrows - 1, x + 1, ncols), SegmLinkVal(dr[0], dsp[0])); } DjSets comps(g.numv); // Find adjacent components for (int v = 0; v < g.numv; ++v) { for (int e_it = g.start[v]; e_it != -1; e_it = g.edges[e_it].next) { int c1 = comps.find(v); int c2 = comps.find(g.edges[e_it].to); if (c1 != c2 && g.edges[e_it].val.dr < hr && g.edges[e_it].val.dsp < hsp) comps.merge(c1, c2); } } vector<SegmLink> edges; edges.reserve(g.numv); // Prepare edges connecting differnet components for (int v = 0; v < g.numv; ++v) { int c1 = comps.find(v); for (int e_it = g.start[v]; e_it != -1; e_it = g.edges[e_it].next) { int c2 = comps.find(g.edges[e_it].to); if (c1 != c2) edges.push_back(SegmLink(c1, c2, g.edges[e_it].val)); } } // Sort all graph's edges connecting differnet components (in asceding order) std::sort(edges.begin(), edges.end()); // Exclude small components (starting from the nearest couple) for (size_t i = 0; i < edges.size(); ++i) { int c1 = comps.find(edges[i].from); int c2 = comps.find(edges[i].to); if (c1 != c2 && (comps.size[c1] < minsize || comps.size[c2] < minsize)) comps.merge(c1, c2); } // Compute sum of the pixel's colors which are in the same segment Mat h_src = src; vector<Vec4i> sumcols(nrows * ncols, Vec4i(0, 0, 0, 0)); for (int y = 0; y < nrows; ++y) { Vec4b *h_srcy = h_src.ptr<Vec4b>(y); for (int x = 0; x < ncols; ++x) { int parent = comps.find(pix(y, x, ncols)); Vec4b col = h_srcy[x]; Vec4i &sumcol = sumcols[parent]; sumcol[0] += col[0]; sumcol[1] += col[1]; sumcol[2] += col[2]; } } // Create final image, color of each segment is the average color of its pixels dst.create(src.size(), src.type()); for (int y = 0; y < nrows; ++y) { Vec4b *dsty = dst.ptr<Vec4b>(y); for (int x = 0; x < ncols; ++x) { int parent = comps.find(pix(y, x, ncols)); const Vec4i &sumcol = sumcols[parent]; Vec4b &dstcol = dsty[x]; dstcol[0] = static_cast<uchar>(sumcol[0] / comps.size[parent]); dstcol[1] = static_cast<uchar>(sumcol[1] / comps.size[parent]); dstcol[2] = static_cast<uchar>(sumcol[2] / comps.size[parent]); } } }
void TestApp::test_vector4(void) { Console::write_line(" Header: cl_vector.h"); Console::write_line(" Class: Vec4"); Console::write_line(" Function: distance3()"); { Vec4d test_a(2.0,3.0,4.0,5.0); Vec4d test_b(3.0,4.0,5.0,6.0); if (test_a.distance3(test_b) != sqrt(1.0 + 1.0 + 1.0 )) fail(); } Console::write_line(" Function: distance4()"); { Vec4d test_a(2.0,3.0,4.0,5.0); Vec4d test_b(3.0,4.0,5.0,6.0); if (test_a.distance4(test_b) != sqrt(1.0 + 1.0 + 1.0 + 1.0 )) fail(); } Console::write_line(" Function: length3()"); { Vec4d testi(3.0,4.0,5.0,6.0); if (testi.length3() != sqrt(50.0 )) fail(); } Console::write_line(" Function: length4()"); { Vec4d testi(3.0,4.0,5.0,6.0); if (testi.length4() != sqrt(86.0 )) fail(); } Console::write_line(" Function: normalize3()"); { Vec4d testi(3.0,4.0,5.0,6.0); testi.normalize3(); if (testi != Vec4d(3.0/sqrt(50.0), 4.0/sqrt(50.0), 5.0/sqrt(50.0), 6.0)) fail(); } Console::write_line(" Function: static normalize3()"); { Vec4d testi(3.0,4.0,5.0,6.0); if (Vec4d::normalize3(testi) != Vec4d(3.0/sqrt(50.0), 4.0/sqrt(50.0), 5.0/sqrt(50.0), 6.0)) fail(); } Console::write_line(" Function: dot3()"); { Vec4d test_a(3.0,4.0,5.0,6.0); Vec4d test_b(13.0,14.0,15.0,16.0); if (test_a.dot3(test_b) != ((3.0 * 13.0)+ (4.0*14.0) + (5.0 * 15.0))) fail(); } Console::write_line(" Function: dot4()"); { Vec4d test_a(3.0,4.0,5.0,6.0); Vec4d test_b(13.0,14.0,15.0,16.0); if (test_a.dot4(test_b) != ((3.0 * 13.0)+ (4.0*14.0) + (5.0 * 15.0) + (6.0 * 16.0))) fail(); } Console::write_line(" Function: normalize4()"); { Vec4d testi(3.0,4.0,5.0,6.0); testi.normalize4(); if (testi != Vec4d(3.0/sqrt(86.0), 4.0/sqrt(86.0), 5.0/sqrt(86.0), 6.0/sqrt(86.0))) fail(); } Console::write_line(" Function: static normalize4()"); { Vec4d testi(3.0,4.0,5.0,6.0); if (Vec4d::normalize4(testi) != Vec4d(3.0/sqrt(86.0), 4.0/sqrt(86.0), 5.0/sqrt(86.0), 6.0/sqrt(86.0))) fail(); } Console::write_line(" Function: operator += (const Vec4<Type>& vector)"); { Vec4d testd(2.5, 3.5, 4.5, 5.5); testd += Vec4d(1.0, 2.0, 3.0, 4.0); if (testd.x != 3.5) fail(); if (testd.y != 5.5) fail(); if (testd.z != 7.5) fail(); if (testd.w != 9.5) fail(); Vec4i testi(2, 3, 4, 5); testi += Vec4i(1, 2, 3, 4); if (testi.x != 3) fail(); if (testi.y != 5) fail(); if (testi.z != 7) fail(); if (testi.w != 9) fail(); } Console::write_line(" Function: operator += ( Type value)"); { Vec4d testd(2.5, 3.5, 4.5, 5.5); double valued = 2.0; testd += valued; if (testd.x != 4.5) fail(); if (testd.y != 5.5) fail(); if (testd.z != 6.5) fail(); if (testd.w != 7.5) fail(); Vec4i testi(2, 3, 4, 5); int valuei = 2; testi += valuei; if (testi.x != 4) fail(); if (testi.y != 5) fail(); if (testi.z != 6) fail(); if (testi.w != 7) fail(); } Console::write_line(" Function: operator + (Type value)"); { Vec4d testd(2.5, 3.5, 4.5, 5.5); double valued = 2.0; testd = testd + valued; if (testd.x != 4.5) fail(); if (testd.y != 5.5) fail(); if (testd.z != 6.5) fail(); if (testd.w != 7.5) fail(); Vec4i testi(2, 3, 4, 5); int valuei = 2; testi = testi + valuei; if (testi.x != 4) fail(); if (testi.y != 5) fail(); if (testi.z != 6) fail(); if (testi.w != 7) fail(); } Console::write_line(" Function: operator + (const Vec4<Type>& vector)"); { Vec4d testd(2.5, 3.5, 4.5, 5.5); testd = testd + Vec4d(1.5, 2.5, 3.5, 4.5); if (testd.x != 4.0) fail(); if (testd.y != 6.0) fail(); if (testd.z != 8.0) fail(); if (testd.w != 10.0) fail(); Vec4i testi(2, 3, 4, 5); testi = testi + Vec4i(1, 2, 3, 4); if (testi.x != 3) fail(); if (testi.y != 5) fail(); if (testi.z != 7) fail(); if (testi.w != 9) fail(); } Console::write_line(" Function: operator -= (const Vec4<Type>& vector)"); { Vec4d testd(2.5, 3.5, 4.5, 5.5); testd -= Vec4d(1.0, 2.0, 3.0, 4.0); if (testd.x != 1.5) fail(); if (testd.y != 1.5) fail(); if (testd.z != 1.5) fail(); if (testd.w != 1.5) fail(); Vec4i testi(2, 3, 4, 5); testi -= Vec4i(1, 2, 3, 4); if (testi.x != 1) fail(); if (testi.y != 1) fail(); if (testi.z != 1) fail(); if (testi.w != 1) fail(); } Console::write_line(" Function: operator -= ( Type value)"); { Vec4d testd(2.5, 3.5, 4.5, 5.5); double valued = 2.0; testd -= valued; if (testd.x != 0.5) fail(); if (testd.y != 1.5) fail(); if (testd.z != 2.5) fail(); if (testd.w != 3.5) fail(); Vec4i testi(2, 3, 4, 5); int valuei = 2; testi -= valuei; if (testi.x != 0) fail(); if (testi.y != 1) fail(); if (testi.z != 2) fail(); if (testi.w != 3) fail(); } Console::write_line(" Function: operator - (Type value)"); { Vec4d testd(2.5, 3.5, 4.5, 5.5); double valued = 2.0; testd = testd - valued; if (testd.x != 0.5) fail(); if (testd.y != 1.5) fail(); if (testd.z != 2.5) fail(); if (testd.w != 3.5) fail(); Vec4i testi(2, 3, 4, 5); int valuei = 2; testi = testi - valuei; if (testi.x != 0) fail(); if (testi.y != 1) fail(); if (testi.z != 2) fail(); if (testi.w != 3) fail(); } Console::write_line(" Function: operator - (const Vec4<Type>& vector)"); { Vec4d testd(2.5, 3.5, 4.5, 5.5); testd = testd - Vec4d(1.5, 2.5, 3.5, 4.5); if (testd.x != 1.0) fail(); if (testd.y != 1.0) fail(); if (testd.z != 1.0) fail(); if (testd.w != 1.0) fail(); Vec4i testi(2, 3, 4, 5); testi = testi - Vec4i(1, 2, 3, 4); if (testi.x != 1) fail(); if (testi.y != 1) fail(); if (testi.z != 1) fail(); if (testi.w != 1) fail(); } Console::write_line(" Function: operator *= (const Vec4<Type>& vector)"); { Vec4d testd(2.5, 3.5, 4.5, 5.5); testd *= Vec4d(1.0, 2.0, 3.0, 4.0); if (testd.x != 2.5) fail(); if (testd.y != 7.0) fail(); if (testd.z != 13.5) fail(); if (testd.w != 22.0) fail(); Vec4i testi(2, 3, 4, 5); testi *= Vec4i(1, 2, 3, 4); if (testi.x != 2) fail(); if (testi.y != 6) fail(); if (testi.z != 12) fail(); if (testi.w != 20) fail(); } Console::write_line(" Function: operator *= ( Type value)"); { Vec4d testd(2.5, 3.5, 4.5, 5.5); double valued = 2.0; testd *= valued; if (testd.x != 5.0) fail(); if (testd.y != 7.0) fail(); if (testd.z != 9.0) fail(); if (testd.w != 11.0) fail(); Vec4i testi(2, 3, 4, 5); int valuei = 2; testi *= valuei; if (testi.x != 4) fail(); if (testi.y != 6) fail(); if (testi.z != 8) fail(); if (testi.w != 10) fail(); } Console::write_line(" Function: operator * (Type value)"); { Vec4d testd(2.5, 3.5, 4.5, 5.5); double valued = 2.0; testd = testd * valued; if (testd.x != 5.0) fail(); if (testd.y != 7.0) fail(); if (testd.z != 9.0) fail(); if (testd.w != 11.0) fail(); Vec4i testi(2, 3, 4, 5); int valuei = 2; testi = testi * valuei; if (testi.x != 4) fail(); if (testi.y != 6) fail(); if (testi.z != 8) fail(); if (testi.w != 10) fail(); } Console::write_line(" Function: operator * (const Vec4<Type>& vector)"); { Vec4d testd(2.5, 3.5, 4.5, 5.5); testd = testd * Vec4d(1.5, 2.5, 3.5, 4.5); if (testd.x != 3.75) fail(); if (testd.y != 8.75) fail(); if (testd.z != 15.75) fail(); if (testd.w != 24.75) fail(); Vec4i testi(2, 3, 4, 5); testi = testi * Vec4i(1, 2, 3, 4); if (testi.x != 2) fail(); if (testi.y != 6) fail(); if (testi.z != 12) fail(); if (testi.w != 20) fail(); } Console::write_line(" Function: operator /= (const Vec4<Type>& vector)"); { Vec4d testd(2.5, 3.5, 4.5, 5.5); testd /= Vec4d(1.0, 2.0, 3.0, 4.0); if (testd.x != 2.5) fail(); if (testd.y != 1.75) fail(); if (testd.z != 1.5) fail(); if (testd.w != 1.375) fail(); Vec4i testi(2, 10, 20, 5); testi /= Vec4i(1, 2, 3, 4); if (testi.x != 2) fail(); if (testi.y != 5) fail(); if (testi.z != 6) fail(); if (testi.w != 1) fail(); } Console::write_line(" Function: operator /= ( Type value)"); { Vec4d testd(2.5, 3.5, 4.5, 5.5); double valued = 2.0; testd /= valued; if (testd.x != 1.25) fail(); if (testd.y != 1.75) fail(); if (testd.z != 2.25) fail(); if (testd.w != 2.75) fail(); Vec4i testi(2, 10, 20, 5); int valuei = 2; testi /= valuei; if (testi.x != 1) fail(); if (testi.y != 5) fail(); if (testi.z != 10) fail(); if (testi.w != 2) fail(); } Console::write_line(" Function: operator / (Type value)"); { Vec4d testd(2.5, 3.5, 4.5, 5.5); double valued = 2.0; testd = testd / valued; if (testd.x != 1.25) fail(); if (testd.y != 1.75) fail(); if (testd.z != 2.25) fail(); if (testd.w != 2.75) fail(); Vec4i testi(2, 10, 20, 5); int valuei = 2; testi = testi / valuei; if (testi.x != 1) fail(); if (testi.y != 5) fail(); if (testi.z != 10) fail(); if (testi.w != 2) fail(); } Console::write_line(" Function: operator / (const Vec4<Type>& vector)"); { Vec4d testd(2.5, 3.5, 4.5, 10.0); testd = testd / Vec4d(1.0, 2.5, 4.5, 2.0); if (testd.x != 2.5) fail(); if (testd.y != 1.4) fail(); if (testd.z != 1.0) fail(); if (testd.w != 5.0) fail(); Vec4i testi(2, 10, 20, 5); testi = testi / Vec4i(1, 2, 3, 4); if (testi.x != 2) fail(); if (testi.y != 5) fail(); if (testi.z != 6) fail(); if (testi.w != 1) fail(); } Console::write_line(" Function: operator = (const Vec4<Type>& vector)"); { Vec4d testd(2.5, 3.5, 4.5, 5.5); testd = Vec4d(1.0, 2.0, 3.0, 4.0); if (testd.x != 1.0) fail(); if (testd.y != 2.0) fail(); if (testd.z != 3.0) fail(); if (testd.w != 4.0) fail(); Vec4i testi(2, 3, 4, 5); testi = Vec4i(1, 2, 3, 4); if (testi.x != 1) fail(); if (testi.y != 2) fail(); if (testi.z != 3) fail(); if (testi.w != 4) fail(); } Console::write_line(" Function: operator == (const Vec4<Type>& vector)"); { Vec4d testd(2.5, 3.5, 4.5, 5.5); if (testd == Vec4d(1.0, 2.0, 3.0, 4.0)) fail(); if (testd == Vec4d(2.5, 2.0, 3.0, 4.0)) fail(); if (testd == Vec4d(2.5, 3.5, 3.0, 4.0)) fail(); if (testd == Vec4d(2.5, 3.5, 4.5, 4.0)) fail(); if (!(testd == Vec4d(2.5, 3.5, 4.5, 5.5))) fail(); Vec4i testi(2, 3, 4, 5); if (testi == Vec4i(1, 2, 3, 4)) fail(); if (testi == Vec4i(2, 2, 3, 4)) fail(); if (testi == Vec4i(2, 3, 3, 4)) fail(); if (testi == Vec4i(2, 3, 4, 4)) fail(); if (!(testi == Vec4i(2, 3, 4, 5))) fail(); } Console::write_line(" Function: operator != (const Vec4<Type>& vector)"); { Vec4d testd(2.5, 3.5, 4.5, 5.5); if (!(testd != Vec4d(1.0, 2.0, 3.0, 4.0))) fail(); if (!(testd != Vec4d(2.5, 2.0, 3.0, 4.0))) fail(); if (!(testd != Vec4d(2.5, 3.5, 3.0, 4.0))) fail(); if (!(testd != Vec4d(2.5, 3.5, 4.5, 4.0))) fail(); if ((testd != Vec4d(2.5, 3.5, 4.5, 5.5))) fail(); Vec4i testi(2, 3, 4, 5); if (!(testi != Vec4i(1, 2, 3, 4))) fail(); if (!(testi != Vec4i(2, 2, 3, 4))) fail(); if (!(testi != Vec4i(2, 3, 3, 4))) fail(); if (!(testi != Vec4i(2, 3, 4, 4))) fail(); if ((testi != Vec4i(2, 3, 4, 5))) fail(); } Console::write_line(" Function: round()"); { Vec4d testd(2.0, 2.5, -2.0, -2.5); testd.round(); if (testd.x != 2.0) fail(); if (testd.y != 3.0) fail(); if (testd.z != -2.0) fail(); if (testd.w != -2.0) fail(); Vec4f testf(2.0f, 2.5f, -2.0f, -2.9f); testf.round(); if (testf.x != 2.0f) fail(); if (testf.y != 3.0f) fail(); if (testf.z != -2.0f) fail(); if (testf.w != -3.0f) fail(); } Console::write_line(" Function: static round()"); { Vec4d testd(2.0, 2.5, -2.0, -2.5); Vec4d destd = Vec4d::round(testd); if (destd.x != 2.0) fail(); if (destd.y != 3.0) fail(); if (destd.z != -2.0) fail(); if (destd.w != -2.0) fail(); Vec4f testf(2.0f, 2.5f, -2.0f, -2.9f); Vec4f destf = Vec4f::round(testf); if (destf.x != 2.0f) fail(); if (destf.y != 3.0f) fail(); if (destf.z != -2.0f) fail(); if (destf.w != -3.0f) fail(); } }
inline size_t Blob::offset(int n, int cn, int row, int col) const { return offset(Vec4i(n, cn, row, col)); }
Vec4i Blob::shape4() const { return Vec4i(num(), channels(), rows(), cols()); }