OpenCV android sdk- extract image under color blob Detection -
i new opencv , want extract parts of image main image, using color blob detection , when user touches part of image select objects particular color. want separate part. how can that?is there way mask , convert mat? here code
the reason want extract part use in calculation.
public class colorblobdetector { // lower , upper bounds range checking in hsv color space private scalar mlowerbound = new scalar(0); private scalar mupperbound = new scalar(0); // minimum contour area in percent contours filtering private static double mmincontourarea = 0.1; // color radius range checking in hsv color space private scalar mcolorradius = new scalar(25,50,50,0); private mat mspectrum = new mat(); private list<matofpoint> mcontours = new arraylist<matofpoint>(); // cache mat mpyrdownmat = new mat(); mat mhsvmat = new mat(); mat mmask = new mat(); mat mdilatedmask = new mat(); mat mhierarchy = new mat(); private mat mmat0; private float[] mbuff; private matofint mhistsize; private matoffloat mranges; private point mp1; private point mp2; private scalar[] mcolorsrgb; private mat mintermediatemat; private scalar mwhilte; private scalar[] mcolorshue; public void setcolorradius(scalar radius) { mcolorradius = radius; } public void sethsvcolor(scalar hsvcolor) { mchannels = new matofint[] { new matofint(0), new matofint(1), new matofint(2) }; mmat0 = new mat(); mintermediatemat = new mat(); mbuff = new float[mhistsizenum]; mhistsize = new matofint(mhistsizenum); mranges = new matoffloat(0f, 256f); mp1 = new point(); mp2 = new point(); mcolorsrgb = new scalar[] { new scalar(200, 0, 0, 255), new scalar(0, 200, 0, 255), new scalar(0, 0, 200, 255) }; mwhilte = scalar.all(255); mcolorshue = new scalar[] { new scalar(255, 0, 0, 255), new scalar(255, 60, 0, 255), new scalar(255, 120, 0, 255), new scalar(255, 180, 0, 255), new scalar(255, 240, 0, 255), new scalar(215, 213, 0, 255), new scalar(150, 255, 0, 255), new scalar(85, 255, 0, 255), new scalar(20, 255, 0, 255), new scalar(0, 255, 30, 255), new scalar(0, 255, 85, 255), new scalar(0, 255, 150, 255), new scalar(0, 255, 215, 255), new scalar(0, 234, 255, 255), new scalar(0, 170, 255, 255), new scalar(0, 120, 255, 255), new scalar(0, 60, 255, 255), new scalar(0, 0, 255, 255), new scalar(64, 0, 255, 255), new scalar(120, 0, 255, 255), new scalar(180, 0, 255, 255), new scalar(255, 0, 255, 255), new scalar(255, 0, 215, 255), new scalar(255, 0, 85, 255), new scalar(255, 0, 0, 255) }; double minh = (hsvcolor.val[0] >= mcolorradius.val[0]) ? hsvcolor.val[0]-mcolorradius.val[0] : 0; double maxh = (hsvcolor.val[0]+mcolorradius.val[0] <= 255) ? hsvcolor.val[0]+mcolorradius.val[0] : 255; mlowerbound.val[0] = minh; mupperbound.val[0] = maxh; mlowerbound.val[1] = hsvcolor.val[1] - mcolorradius.val[1]; mupperbound.val[1] = hsvcolor.val[1] + mcolorradius.val[1]; mlowerbound.val[2] = hsvcolor.val[2] - mcolorradius.val[2]; mupperbound.val[2] = hsvcolor.val[2] + mcolorradius.val[2]; mlowerbound.val[3] = 0; mupperbound.val[3] = 255; mat spectrumhsv = new mat(1, (int)(maxh-minh), cvtype.cv_8uc3); (int j = 0; j < maxh-minh; j++) { byte[] tmp = {(byte)(minh+j), (byte)255, (byte)255}; spectrumhsv.put(0, j, tmp); } imgproc.cvtcolor(spectrumhsv, mspectrum, imgproc.color_hsv2rgb_full, 4); } public mat getspectrum() { return mspectrum; } public void setmincontourarea(double area) { mmincontourarea = area; } public void process(mat rgbaimage) { imgproc.pyrdown(rgbaimage, mpyrdownmat); imgproc.pyrdown(mpyrdownmat, mpyrdownmat); imgproc.cvtcolor(mpyrdownmat, mhsvmat, imgproc.color_rgb2hsv_full); core.inrange(mhsvmat, mlowerbound, mupperbound, mmask); imgproc.dilate(mmask, mdilatedmask, new mat()); list<matofpoint> contours = new arraylist<matofpoint>(); imgproc.findcontours(mdilatedmask, contours, mhierarchy, imgproc.retr_external, imgproc.chain_approx_simple); // find max contour area double maxarea = 0; iterator<matofpoint> each = contours.iterator(); while (each.hasnext()) { matofpoint wrapper = each.next(); double area = imgproc.contourarea(wrapper); if (area > maxarea) maxarea = area; } // filter contours area , resize fit original image size mcontours.clear(); each = contours.iterator(); while (each.hasnext()) { matofpoint contour = each.next(); if (imgproc.contourarea(contour) > mmincontourarea*maxarea) { core.multiply(contour, new scalar(4,4), contour); mcontours.add(contour); } } } private int mhistsizenum = 25; private matofint mchannels[]; public void functionsepratergb(size sizergba, mat rgba){ mat hist = new mat(); int thikness = (int) (sizergba.width / (mhistsizenum + 10) / 5); if(thikness > 5) thikness = 5; int offset = (int) ((sizergba.width - (5*mhistsizenum + 4*10)*thikness)/2); // rgb for(int c=0; c<3; c++) { imgproc.calchist(arrays.aslist(rgba), mchannels[c], mmat0, hist, mhistsize, mranges); core.normalize(hist, hist, sizergba.height/2, 0, core.norm_inf); hist.get(0, 0, mbuff); for(int h=0; h<mhistsizenum; h++) { mp1.x = mp2.x = offset + (c * (mhistsizenum + 10) + h) * thikness; mp1.y = sizergba.height-1; mp2.y = mp1.y - 2 - (int)mbuff[h]; imgproc.line(rgba, mp1, mp2, mcolorsrgb[c], thikness); } } // value , hue imgproc.cvtcolor(rgba, mintermediatemat, imgproc.color_rgb2hsv_full); // value imgproc.calchist(arrays.aslist(mintermediatemat), mchannels[2], mmat0, hist, mhistsize, mranges); core.normalize(hist, hist, sizergba.height/2, 0, core.norm_inf); hist.get(0, 0, mbuff); for(int h=0; h<mhistsizenum; h++) { mp1.x = mp2.x = offset + (3 * (mhistsizenum + 10) + h) * thikness; mp1.y = sizergba.height-1; mp2.y = mp1.y - 2 - (int)mbuff[h]; imgproc.line(rgba, mp1, mp2, mwhilte, thikness); } // hue imgproc.calchist(arrays.aslist(mintermediatemat), mchannels[0], mmat0, hist, mhistsize, mranges); core.normalize(hist, hist, sizergba.height/2, 0, core.norm_inf); hist.get(0, 0, mbuff); for(int h=0; h<mhistsizenum; h++) { mp1.x = mp2.x = offset + (4 * (mhistsizenum + 10) + h) * thikness; mp1.y = sizergba.height-1; mp2.y = mp1.y - 2 - (int)mbuff[h]; imgproc.line(rgba, mp1, mp2, mcolorshue[h], thikness); } } public list<matofpoint> getcontours() { return mcontours; } } thanks much
Comments
Post a Comment