public SaliencyResult saliencyalgorithmInterface(ImageObj imgobj, String method) { // TODO Auto-generated method stub float min = Float.MAX_VALUE; float max = Float.MIN_VALUE; String imgpath = imgobj.getSourcePath(); int k_num = imgobj.getK_num(); SaliencyResult result = new SaliencyResult(); Mat img = Highgui.imread(imgpath, Highgui.CV_LOAD_IMAGE_GRAYSCALE); Mat saliencyMap = new Mat(); saliencyMap.create(img.rows(), img.cols(), CvType.CV_16U); int HistGram[] = new int[256]; int Gray[] = new int[img.cols() * img.rows()]; int Dist[] = new int[256]; float DistMap[] = new float[img.rows() * img.cols()]; for (int row = 0; row < img.rows(); row++) { int CurIndex = row * img.cols(); for (int col = 0; col < img.cols(); col++) { HistGram[(int) (img.get(row, col)[0])]++; Gray[CurIndex] = (int) (img.get(row, col)[0]); CurIndex++; } } for (int Y = 0; Y < 256; Y++) { int Value = 0; for (int X = 0; X < 256; X++) Value += Math.abs(Y - X) * HistGram[X]; Dist[Y] = Value; } for (int row = 0; row < img.rows(); row++) { int CurIndex = row * img.cols(); for (int col = 0; col < img.cols(); col++) { DistMap[CurIndex] = Dist[Gray[CurIndex]]; if (DistMap[CurIndex] < min) min = DistMap[CurIndex]; if (DistMap[CurIndex] > max) max = DistMap[CurIndex]; CurIndex++; } } for (int row = 0; row < img.rows(); row++) { int CurIndex = row * img.cols(); for (int col = 0; col < img.cols(); col++) { saliencyMap.put(row, col, partTwo((DistMap[CurIndex] - min) / (max - min) * 255)); CurIndex++; } } new findMarkUtil(); int nums[] = null; if (method == "kmeans") { nums = findMarkUtil.findMarkUtil_kmeans(saliencyMap, k_num, 255, 0, 5); } else if (method == "random") { nums = findMarkUtil.findMarkUtil_random(saliencyMap, k_num, 255); } result.setK_num(k_num); result.setSource(imgpath); result.setResult(nums); result.setSaliency(saliencyMap); return result; }
public static void test3() { MatchContext context; context = new MatchContext(new EnsembleAlgorithm()); ImageObj imgobj = new ImageObj(); imgobj.setSource("D:1.png"); imgobj.setTemplate("D:a.png"); imgobj.setStartx(354); imgobj.setStarty(24); imgobj.setEndx(515); imgobj.setEndy(87); imgobj.setWidth(540); imgobj.setHeight(960); MatchResult result = context.contextInterface(imgobj); result.dispResult(); imgobj.writeResult(result, "result.jpg"); }
public static void test1() { MatchContext context; context = new MatchContext(new EnsembleAlgorithm()); ImageObj imgobj = new ImageObj(); imgobj.setSource("D:\\itestin\\512.png"); imgobj.setTemplate("D:\\itestin\\512a.png"); imgobj.setStartx(376); imgobj.setStarty(172); imgobj.setEndx(595); imgobj.setEndy(269); imgobj.setWidth(540); imgobj.setHeight(960); MatchResult result = context.contextInterface(imgobj); result.dispResult(); imgobj.writeResult(result, "result.jpg"); }
private void loadImgBy(String url, ImageView v, int defaultDrawable) { mImageView = v; mUrl = url; if (TextUtils.isEmpty(url)) { mImageView.setTag(R.layout.main, mUrl); mImageView.setImageResource(defaultDrawable); return; } File cacheDir = new File(Configs.tlcyImagePath); String urlConvert = MD5.md5Upper(mUrl); File file = new File(cacheDir, urlConvert); if (file.exists()) { if (mType == 1) { mImageView.setImageBitmap(findScaleBitmap()); } else { mImageView.setImageBitmap(loadImgLocal(mUrl)); } /* * new Thread(){ public void run() { bitmapReference = new * SoftReference<Bitmap>(getBitmapFromLocal(mUrl)); if (mType == 1) * { bitmapReference = new SoftReference<Bitmap>(findScaleBitmap()); * } mImageView.setTag(R.layout.main, mUrl); * LogInfo.LogOut("file.exists.positin=" * +pos+" bitMap="+getBitmap()+ * " bitmapReference="+bitmapReference); * loaderImageHandler.sendEmptyMessage(1); }; }.start(); */ } else { ImageObj obj = new ImageObj(); obj.imageView = mImageView; obj.imageView.setTag(R.layout.main, mUrl); obj.imageView.setImageResource(defaultDrawable); obj.type = mType; if (map.containsKey(mUrl)) { // 防止重复请求同一张图片 map.get(mUrl).add(obj); LogInfo.LogOut( "list", "ImageLoader.igrThread\t\tmImageView=" + mImageView + "\t\turl=" + mUrl); return; } else { ArrayList<ImageObj> list = new ArrayList<ImageObj>(); list.add(obj); map.put(mUrl, list); } new Thread() { @Override public void run() { try { HttpURLConnection conn = null; InputStream is = null; try { if (!mAllowLoad) { LogInfo.LogOut("list", "-----------------------prepare to load"); synchronized (lock) { try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } if (mAllowLoad && (firstLoad || (pos <= mStopLoadLimit && pos >= mStartLoadLimit)) || pos == -1) { LogInfo.LogOut( "list", "ImageLoader.thread=" + this.getName() + "\t\tmImageView=" + mImageView + "\t\turl=" + mUrl); URL tempUrl = new URL(mUrl); conn = (HttpURLConnection) tempUrl.openConnection(); conn.setDoInput(true); LogInfo.LogOut("list", "--conn.getResponseCode() = " + conn.getResponseCode()); if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { is = conn.getInputStream(); byte buffer[] = new byte[1024]; int len; ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); while ((len = is.read(buffer)) != -1) { swapStream.write(buffer, 0, len); } byte[] swapArray = swapStream.toByteArray(); Bitmap bitmap = BitmapFactory.decodeByteArray(swapArray, 0, swapArray.length); swapStream.close(); bitmapReference = new SoftReference<Bitmap>(bitmap); saveBitmapToLocal(mUrl, bitmap); } } else { LogInfo.LogOut("igrPosition=" + pos); } if (getBitmap() != null) { if (mType == 1 && getBitmap().getHeight() > 0) { findScaleBitmap(); // bitmapReference = new // SoftReference<Bitmap>(findScaleBitmap()); } loaderImageHandler.sendEmptyMessage(0); } else { map.remove(mUrl); } } catch (Exception e) { map.remove(mUrl); e.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { } } if (conn != null) { conn.disconnect(); } } } catch (OutOfMemoryError e) { System.gc(); } } }.start(); } }