Grabcutopencvを使用した画像セグメンテーション用の次のコードがあります。私は初めてopencvを使用しています。 opencvライブラリを追加しました。他の人が完全に働いている間、highguiはまだシンボルを解決できないことを示しています。何か他のものを追加する必要がありますか? (あなたもコードを手伝ってくれるなら、追加の助けになるでしょう)ここにコードがあります:編集:私は更新されたコードを投稿しました:
package com.example.Android.seg;
import Android.support.v7.app.ActionBarActivity;
import Android.os.Bundle;
import Android.util.Log;
import Android.view.Menu;
import Android.view.MenuItem;
import Java.io.IOException;
import org.opencv.Android.OpenCVLoader;
import org.opencv.Android.Utils;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;
import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs; // imread, imwrite, etc
// VideoCapture
import Android.app.Activity;
import Android.graphics.Bitmap;
import Android.graphics.BitmapFactory;
import Android.graphics.Canvas;
import Android.view.Gravity;
import Android.view.MotionEvent;
import Android.view.View;
import Android.view.View.OnTouchListener;
import Android.widget.ImageView;
import Android.widget.Toast;
import Android.support.v7.app.ActionBarActivity;
import Android.os.Bundle;
import Android.view.Menu;
import Android.view.MenuItem;
public class seg extends Activity implements OnTouchListener {
static{
if (!OpenCVLoader.initDebug()) {
Log.i("TEST", "Cannot connect to OpenCV Manager");
// Handle initialization error
}
else {
Log.i("TEST", "Connected to OpenCV Manager");
}
}
ImageView imageView;
Bitmap bitmap;
Canvas canvas;
Scalar color = new Scalar(255, 0, 0, 255);
Point tl, br;
int counter;
Bitmap bitmapResult, bitmapBackground;
Mat dst = new Mat();
//"sdcard/DCIM/wall.jpg"
final String pathToImage = "wall.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seg);
imageView = (ImageView) this.findViewById(R.id.imageView);
bitmap = BitmapFactory.decodeFile(pathToImage);
Toast msg = Toast.makeText(seg.this, "Press top left and bottom right of the foreground image", Toast.LENGTH_LONG);
msg.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
msg.show();
bitmapResult = bitmap.copy(bitmap.getConfig(), true);
canvas = new Canvas(bitmapResult);
imageView.setImageBitmap(bitmapResult);
imageView.setOnTouchListener(this);
tl = new Point();
br = new Point();
counter = 0;
}
//@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (counter == 0) {
tl.x = event.getX();
tl.y = event.getY();
counter++;
} else if (counter == 1) {
br.x = event.getX();
br.y = event.getY();
counter++;
Mat img = new Mat();
//val mat = Imgcodecs.imread(pathToImage)
img = Imgcodecs.imread(pathToImage);
Mat background = new Mat();
try {
background = Utils.loadResource(getApplicationContext(),
R.drawable.wall );
} catch (IOException e) {
e.printStackTrace();
}
backgroundSubtracting(img, background);
Imgcodecs.imwrite("wall.jpg", dst);
//Highgui.imwrite("/mnt/sdcard/GRABCUT/rect.png", dst);
Bitmap jpg = BitmapFactory
.decodeFile("wall.jpg");
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setAdjustViewBounds(true);
imageView.setPadding(2, 2, 2, 2);
imageView.setImageBitmap(jpg);
imageView.invalidate();
}
}
return true;
}
private void backgroundSubtracting(Mat img, Mat background) {
Mat firstMask = new Mat();
Mat bgModel = new Mat();
Mat fgModel = new Mat();
Mat mask;
Mat source = new Mat(1, 1, CvType.CV_8U, new Scalar(3.0));
dst = new Mat();
Rect rect = new Rect(tl, br);
Imgproc.grabCut(img, firstMask, rect, bgModel, fgModel, 1, 0 /* GC_INIT_WITH_RECT */);
Core.compare(firstMask, source/* GC_PR_FGD */, firstMask, Core.CMP_EQ);
Mat foreground = new Mat(img.size(), CvType.CV_8UC3, new Scalar(255,
255, 255));
img.copyTo(foreground, firstMask);
Imgproc.rectangle(img, tl, br, color);
//Core.rectangle(img, tl, br, color);
Mat tmp = new Mat();
Imgproc.resize(background, tmp, img.size());
background = tmp;
mask = new Mat(foreground.size(), CvType.CV_8UC1, new Scalar(255, 255, 255));
Imgproc.cvtColor(foreground, mask, 6/* COLOR_BGR2GRAY */);
Imgproc.threshold(mask, mask, 254, 255, 1 /* THRESH_BINARY_INV */);
Mat vals = new Mat(1, 1, CvType.CV_8UC3, new Scalar(0.0));
background.copyTo(dst);
background.setTo(vals, mask);
Core.add(background, foreground, dst, mask);
firstMask.release();
source.release();
bgModel.release();
fgModel.release();
vals.release();
}
}
これが私のlogcatです:
05-20 09:48:51.830 24423-24423/com.example.Android.seg D/dalvikvm﹕ Late-enabling CheckJNI
05-20 09:48:51.832 24423-24429/com.example.Android.seg D/dalvikvm﹕ threadid=2: interp stack at 0x40000000
05-20 09:48:51.840 24423-24431/com.example.Android.seg D/dalvikvm﹕ threadid=3: interp stack at 0x40008000
05-20 09:48:51.840 24423-24423/com.example.Android.seg D/jdwp﹕ prepping for JDWP over ADB
05-20 09:48:51.840 24423-24423/com.example.Android.seg D/jdwp﹕ ADB transport startup
05-20 09:48:51.841 24423-24432/com.example.Android.seg D/dalvikvm﹕ threadid=4: interp stack at 0x49247000
05-20 09:48:51.841 24423-24432/com.example.Android.seg D/jdwp﹕ JDWP: thread running
05-20 09:48:51.841 24423-24432/com.example.Android.seg D/jdwp﹕ acceptConnection
05-20 09:48:51.842 24423-24432/com.example.Android.seg D/jdwp﹕ trying to receive file descriptor from ADB
05-20 09:48:51.842 24423-24433/com.example.Android.seg D/dalvikvm﹕ threadid=5: interp stack at 0x4bd75000
05-20 09:48:51.851 24423-24432/com.example.Android.seg D/jdwp﹕ received file descriptor 40 from ADB
05-20 09:48:51.853 24423-24435/com.example.Android.seg D/dalvikvm﹕ threadid=6: interp stack at 0x4bd7d000
05-20 09:48:51.854 24423-24435/com.example.Android.seg D/dalvikvm﹕ threadid=6: calling run()
05-20 09:48:51.855 24423-24432/com.example.Android.seg D/jdwp﹕ processIncoming
05-20 09:48:51.855 24423-24432/com.example.Android.seg D/jdwp﹕ processIncoming
05-20 09:48:51.855 24423-24432/com.example.Android.seg D/jdwp﹕ handlePacket : cmd=0x1, cmdSet=0xC7, len=0x13, id=0x40000044, flags=0x0, dataLen=0x8
05-20 09:48:51.856 24423-24432/com.example.Android.seg D/jdwp﹕ REQ: DDM.Chunk (cmd=199/1 dataLen=8 id=0x40000044)
05-20 09:48:51.857 24423-24432/com.example.Android.seg D/jdwp﹕ reply: dataLen=9 err=NONE(0)
05-20 09:48:51.857 24423-24432/com.example.Android.seg D/jdwp﹕ processIncoming
05-20 09:48:51.857 24423-24432/com.example.Android.seg D/jdwp﹕ handlePacket : cmd=0x1, cmdSet=0xC7, len=0x17, id=0x40000045, flags=0x0, dataLen=0xC
05-20 09:48:51.857 24423-24432/com.example.Android.seg D/jdwp﹕ REQ: DDM.Chunk (cmd=199/1 dataLen=12 id=0x40000045)
05-20 09:48:51.859 24423-24432/com.example.Android.seg D/jdwp﹕ reply: dataLen=50 err=NONE(0)
05-20 09:48:51.860 24423-24432/com.example.Android.seg D/jdwp﹕ processIncoming
05-20 09:48:51.860 24423-24432/com.example.Android.seg D/jdwp﹕ handlePacket : cmd=0x1, cmdSet=0xC7, len=0x13, id=0x40000046, flags=0x0, dataLen=0x8
05-20 09:48:51.860 24423-24432/com.example.Android.seg D/jdwp﹕ REQ: DDM.Chunk (cmd=199/1 dataLen=8 id=0x40000046)
05-20 09:48:51.862 24423-24432/com.example.Android.seg D/jdwp﹕ reply: dataLen=216 err=NONE(0)
05-20 09:48:51.862 24423-24432/com.example.Android.seg D/jdwp﹕ processIncoming
05-20 09:48:51.862 24423-24432/com.example.Android.seg D/jdwp﹕ handlePacket : cmd=0x1, cmdSet=0xC7, len=0x13, id=0x40000047, flags=0x0, dataLen=0x8
05-20 09:48:51.863 24423-24432/com.example.Android.seg D/jdwp﹕ REQ: DDM.Chunk (cmd=199/1 dataLen=8 id=0x40000047)
05-20 09:48:51.863 24423-24432/com.example.Android.seg D/jdwp﹕ reply: dataLen=9 err=NONE(0)
05-20 09:48:51.863 24423-24432/com.example.Android.seg D/jdwp﹕ processIncoming
05-20 09:48:51.863 24423-24432/com.example.Android.seg D/jdwp﹕ handlePacket : cmd=0x1, cmdSet=0xC7, len=0x14, id=0x40000048, flags=0x0, dataLen=0x9
05-20 09:48:51.863 24423-24432/com.example.Android.seg D/jdwp﹕ REQ: DDM.Chunk (cmd=199/1 dataLen=9 id=0x40000048)
05-20 09:48:51.863 24423-24432/com.example.Android.seg D/jdwp﹕ reply: dataLen=0 err=NONE(0)
05-20 09:48:51.863 24423-24436/com.example.Android.seg D/dalvikvm﹕ threadid=7: interp stack at 0x4bd85000
05-20 09:48:51.864 24423-24436/com.example.Android.seg D/dalvikvm﹕ threadid=7: calling run()
05-20 09:48:51.872 24423-24437/com.example.Android.seg D/dalvikvm﹕ threadid=8: interp stack at 0x4bd8d000
05-20 09:48:51.873 24423-24437/com.example.Android.seg D/dalvikvm﹕ threadid=8: calling run()
05-20 09:48:52.050 24423-24439/com.example.Android.seg D/dalvikvm﹕ threadid=9: interp stack at 0x4bd95000
05-20 09:48:52.052 24423-24440/com.example.Android.seg D/dalvikvm﹕ threadid=10: interp stack at 0x4bd9d000
05-20 09:48:52.064 24423-24423/com.example.Android.seg D/jdwp﹕ sendBufferedRequest : len=0x39
05-20 09:48:52.918 24423-24423/com.example.Android.seg D/jdwp﹕ sendBufferedRequest : len=0x45
05-20 09:48:53.289 24423-24423/com.example.Android.seg D/ActivityThread﹕ BIND_APPLICATION handled : 0 / AppBindData{appInfo=ApplicationInfo{41615870 com.example.Android.seg}}
05-20 09:48:53.296 24423-24423/com.example.Android.seg D/OpenCV/StaticHelper﹕ Trying to get library list
05-20 09:48:53.296 24423-24423/com.example.Android.seg D/dalvikvm﹕ Trying to load lib /mnt/asec/com.example.Android.seg-2/lib/libopencv_info.so 0x41618e00
05-20 09:48:53.297 24423-24423/com.example.Android.seg A/libc﹕ Fatal signal 7 (SIGBUS) at 0x4bf6d9b8 (code=2)
opencv3.0では、Javaにhighguiモジュールはもうありません。
機能は新しいvideoio
モジュールとimgcodecs
(未読のモジュール)に分割されました。
_import org.opencv.core.*;
import org.opencv.imgcodecs; // imread, imwrite, etc
import org.opencv.videoio; // VideoCapture
_
編集:img=Highgui.imread(pathtoimage);
をimg = Imgcodecs.imread(pathtoimage);
に変更します
およびCore.rectangle(img, tl, br, color);
からImgproc.rectangle(img, t1, br, color);
また、これらのエラーを解決するために OpenCV 3.0モジュール を通過することをお勧めします。
ファイル構造によっては、代わりに次のようになります。
import org.opencv.imgcodecs.Imgcodecs;
OpenCV 3.0以降にはHighguiモジュールはありませんが、代わりに同じImgcodecsモジュールを使用できます
古いもの
Highgui.imread(fileName, Highgui.CV_LOAD_IMAGE_GRAYSCALE)
Highgui.imread(fileName)
新しい物
Imgcodecs.imread(fileName, Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE)
Imgcodecs.imread(fileName)