I tried to implement the KNN findNearest function. My program shall recognize numbers in a picture, but I get an error if the program uses findNearest. Here's the code:
`private void searchingData() {
img_gray = new Mat();
img_blur = new Mat();
img_thres = new Mat();
Imgproc.cvtColor(img, img_gray, Imgproc.COLOR_BGR2GRAY);
Imgproc.GaussianBlur(img_gray, img_blur, new Size(5,5), 0);
//Imgproc.adaptiveThreshold(img_blur, img_thres, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 7, 5);
Imgproc.Canny(img_blur, img_thres, 10, 100);
Imgproc.findContours(img_thres, contours, new Mat(), Imgproc.RETR_LIST,Imgproc.CHAIN_APPROX_SIMPLE);
for(int i=0; i< contours.size();i++){
Rect rect = Imgproc.boundingRect(contours.get(i));
if (rect.height < 50 && rect.height > 20){
System.out.println(rect.x +","+rect.y+","+rect.height+","+rect.width);
Mat subImg = new Mat();
Imgproc.resize(img.submat(rect), subImg, new Size(10,10));
//Found numbers then try to recognize it
recognize(subImg);
}
}
}
public void learn() { //get the training data and train the KNN
Mat sample, training_img = new Mat(), res = new Mat();
for (int i = 1; i < 10; i++) {
String path = String.format(".../Documents/numbers/%03d.png", i);
sample = Imgcodecs.imread(path, Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
Mat m = new Mat(new Size(1,1), CvType.CV_32FC1);
m.setTo(new Scalar(i));
res.push_back(m);
training_img.push_back(prepareImg(sample));
}
model = KNearest.create();
model.train(training_img, Ml.ROW_SAMPLE, res);
}
private Mat prepareImg(Mat img) { //change image in the right format
Mat sample = new Mat(), sized = new Mat();
Imgproc.resize(img, sized, new Size(10,10));
sized.reshape(1,1).convertTo(sample, CvType.CV_32FC1);
return sample;
}
public void recognize(Mat getImg) {
Mat results = new Mat();
Mat dists = new Mat();
float result = model.findNearest(prepareImg(getImg), 2, results, new Mat(), dists);
//result should be the number in getImg
System.out.println(result);
}`
OpenCV Error: Assertion failed (test_samples.type() == CV_32F &&
test_samples.cols == samples.cols) in
cv::ml::BruteForceImpl::findNearest, file
C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\ml\src\knearest.cpp,
line 325 Exception in thread "main" CvException
[org.opencv.core.CvException: cv::Exception:
C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\ml\src\knearest.cpp:325:
error: (-215) test_samples.type() == CV_32F && test_samples.cols ==
samples.cols in function cv::ml::BruteForceImpl::findNearest ]
Thanks a lot!
Related
I'd like to copy the recognized text to a clean image using createBitmap but I dont know how to get it from the boxes on the original image.
This code finds Maximally stable extremal regions and highlights them on the bitmap. Further below is what I get wit it on a sample image.
private void sgmnt(Mat mImg) {
Imgproc.cvtColor(msImg, m2, Imgproc.COLOR_RGB2GRAY);
Mat mRgba = mImg;
Mat mGray = m2;
Scalar CONTOUR_COLOR = new Scalar(1, 1, 255, 1);
//Scalar CONTOUR_COLOR = new Scalar(255);
MatOfKeyPoint keyPoint = new MatOfKeyPoint();
List<KeyPoint> listPoint = new ArrayList<>();
KeyPoint kPoint = new KeyPoint();
Mat mask = Mat.zeros(mGray.size(), CvType.CV_8UC1);
int rectanx1;
int rectany1;
int rectanx2;
int rectany2;
int imgSize = mGray.height() * mGray.width();
Scalar zeros = new Scalar(255,1,1, 1);
List<MatOfPoint> contour2 = new ArrayList<MatOfPoint>();
Mat kernel = new Mat(1, 50, CvType.CV_8UC1, Scalar.all(255));
Mat morByte = new Mat();
Mat hierarchy = new Mat();
Rect rectan3 = new Rect();
FeatureDetector detector = FeatureDetector.create(FeatureDetector.MSER);
detector.detect(mGray, keyPoint);
listPoint = keyPoint.toList();
for(int ind = 0; ind < listPoint.size(); ++ind) {
kPoint = listPoint.get(ind);
rectanx1 = (int) (kPoint.pt.x - 0.5 * kPoint.size);
rectany1 = (int) (kPoint.pt.y - 0.5 * kPoint.size);
rectanx2 = (int) (kPoint.size);
rectany2 = (int) (kPoint.size);
if (rectanx1 <= 0) {
rectanx1 = 1;
}
if (rectany1 <= 0) {
rectany1 = 1;
}
if ((rectanx1 + rectanx2) > mGray.width()) {
rectanx2 = mGray.width() - rectanx1;
}
if ((rectany1 + rectany2) > mGray.height()) {
rectany2 = mGray.height() - rectany1;
}
Rect rectant = new Rect(rectanx1, rectany1, rectanx2, rectany2);
try{
Mat roi = new Mat(mask, rectant);
roi.setTo(CONTOUR_COLOR);
}
catch (Exception ex) {
Log.d("mylog", "mat roi error " + ex.getMessage());
}
}
Imgproc.morphologyEx(mask, morByte, Imgproc.MORPH_DILATE, kernel);
Imgproc.findContours(morByte, contour2, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE);
for(int i = 0; i<contour2.size(); ++i){
rectan3 = Imgproc.boundingRect(contour2.get(i));
if(rectan3.area() > 0.5 * imgSize || rectan3.area()<100 || rectan3.width / rectan3.height < 2){
Mat roi = new Mat(morByte, rectan3);
roi.setTo(zeros);
}else{
Imgproc.rectangle(mRgba, rectan3.br(), rectan3.tl(), CONTOUR_COLOR);
}
}
}
Here's an example of what I get:
My problem is I want to somehow get the text inside the boxes.
I'm doing a project using opencv in android studio.
My project is about comparing colour histogram of an input image captured by camera with the 6 images that is stored in the drawable. The opencv function that I used is comparHist(). The purpose of the comparison is to know if the input image has matched colour to any images in the drawable.
My problem is I need to calculate colour histogram using calcHist() for every 6 images every time there is an input image(captured by camera) so that I can compare them. I was thinking to store the colour histogram value of 6 images in an array. Can I do so?
I was searching for a solution but there is none. Here is my code that I have done.
public int resultRecog() {
drawImg = BitmapFactory.decodeResource(getResources(), R.drawable.onefront);
Mat hsv_base = new Mat();
Mat hsv_test = new Mat();
Utils.bitmapToMat(drawImg, hsv_base);
Utils.bitmapToMat(pic, hsv_test);
/// Convert to HSV
Imgproc.cvtColor(hsv_base, hsv_base, Imgproc.COLOR_BGR2HSV);
Imgproc.cvtColor(hsv_test, hsv_test, Imgproc.COLOR_BGR2HSV);
/// h s bins value
MatOfInt histSize = new MatOfInt(40, 40);
//// ranges h=0-180, s=0-256
MatOfFloat ranges = new MatOfFloat(0f, 180f, 0f, 256f);
/// two channel
MatOfInt channels = new MatOfInt(0, 1);
Mat hist_base = new Mat();
Mat hist_test = new Mat();
ArrayList<Mat> histImages = new ArrayList<Mat>();
histImages.add(hsv_base);
Imgproc.calcHist(histImages, channels, new Mat(), hist_base, histSize, ranges, false);
Core.normalize(hist_base, hist_base, 0, 1, Core.NORM_MINMAX, -1, new Mat());
histImages = new ArrayList<Mat>();
histImages.add(hsv_test);
Imgproc.calcHist(histImages, channels, new Mat(), hist_test, histSize, ranges, false);
Core.normalize(hist_test, hist_test, 0, 1, Core.NORM_MINMAX, -1, new Mat());
double result = Imgproc.compareHist(hist_base, hist_test, 0);
drawImg.recycle();
drawImg = null;
int r;
if (result >= 0.15) {
r = 1;
} else {
drawImg = BitmapFactory.decodeResource(getResources(), R.drawable.fivefront);
Mat hsv_base5 = new Mat();
Utils.bitmapToMat(drawImg, hsv_base5);
Imgproc.cvtColor(hsv_base5, hsv_base5, Imgproc.COLOR_BGR2HSV);
Mat hist_base5 = new Mat();
ArrayList<Mat> histImages5 = new ArrayList<Mat>();
histImages5.add(hsv_base5);
Imgproc.calcHist(histImages5, channels, new Mat(), hist_base5, histSize, ranges, false);
Core.normalize(hist_base5, hist_base5, 0, 1, Core.NORM_MINMAX, -1, new Mat());
result = Imgproc.compareHist(hist_base5, hist_test, 0);
drawImg.recycle();
drawImg = null;
if (result >= 0.1) {
r = 5;
} else {
drawImg = BitmapFactory.decodeResource(getResources(), R.drawable.f10);
Mat hsv_base10 = new Mat();
Utils.bitmapToMat(drawImg, hsv_base10);
Imgproc.cvtColor(hsv_base10, hsv_base10, Imgproc.COLOR_BGR2HSV);
Mat hist_base10 = new Mat();
ArrayList<Mat> histImages10 = new ArrayList<Mat>();
histImages10.add(hsv_base10);
Imgproc.calcHist(histImages10, channels, new Mat(), hist_base10, histSize, ranges, false);
Core.normalize(hist_base10, hist_base10, 0, 1, Core.NORM_MINMAX, -1, new Mat());
result = Imgproc.compareHist(hist_base10, hist_test, 0);
drawImg.recycle();
drawImg = null;
if (result >= 0.1) {
r = 10;
} else {
drawImg = BitmapFactory.decodeResource(getResources(), R.drawable.fiftyfront);
Mat hsv_base50 = new Mat();
Utils.bitmapToMat(drawImg, hsv_base50);
Imgproc.cvtColor(hsv_base50, hsv_base50, Imgproc.COLOR_BGR2HSV);
Mat hist_base50 = new Mat();
ArrayList<Mat> histImages50 = new ArrayList<Mat>();
histImages50.add(hsv_base50);
Imgproc.calcHist(histImages50, channels, new Mat(), hist_base50, histSize, ranges, false);
Core.normalize(hist_base50, hist_base50, 0, 1, Core.NORM_MINMAX, -1, new Mat());
result = Imgproc.compareHist(hist_base50, hist_test, 0);
drawImg.recycle();
drawImg = null;
if (result >= 0.1) {
r = 50;
} else {
r = 0;
}
}//rm10
}//rm5
}//rm1
return r;
}
So my question is, can I store the colour histogram value (of 6 images stored in the drawable) in an array so that the application doesn't need to re-calculate the colour histogram every time there is input image captured by camera
I am working on a licence plate recognition software using OpenCV, Tesseract and Java but experiencing issues, I cant seem to segment my text correctly, its not always that I get all characters to be detected and bounded with a bounding box these are some of my outputs with my code...and also when I detect the characters I never know which character is in which box so when I pass them through tesseract they get jumbled, how do I format my string?
This one fails despite the clearly visible characters:
Here Z and 6 fail to be detected even when 6 is clearly visible:
Below is my code:
originalFrame = image.clone();
roiColor = image.clone();
Imgproc.cvtColor(image, image, Imgproc.COLOR_BGR2GRAY, 0);
originalFrameGrayScale = image.clone();
Mat morph = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(9, 9));
Imgproc.morphologyEx(image, image, Imgproc.MORPH_TOPHAT, morph);
Imgproc.Sobel(image, image, -1, 2, 0);
Imgproc.GaussianBlur(image, image, new Size(5,5), 3,3);
Imgproc.morphologyEx(image, image, Imgproc.MORPH_CLOSE, morph);
Imgproc.threshold(image, image, 200, 255, Imgproc.THRESH_OTSU);
Vector<Rect> rectangles = detectionContour(image);
Mat roi = originalFrameGrayScale.clone();
if(!rectangles.isEmpty()){
roi = originalFrameGrayScale.submat(rectangles.get(0));
roiBlack = roi.clone();
roiColor = roiColor.submat(rectangles.get(0));
Imgproc.rectangle(originalFrame, rectangles.get(0).br(), rectangles.get(0).tl(), new Scalar(0,0,255), 2);
}
Imgproc.medianBlur(roi, roi, 3);
Imgproc.adaptiveThreshold(roi, roi, 225, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY, 15, 3);
roiBinarize = roi.clone();
Mat erode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1, 1));
Mat dilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT,new Size(1, 1));
Imgproc.morphologyEx(roi, roi, Imgproc.MORPH_OPEN, dilate);
Imgproc.morphologyEx(roi, roi, Imgproc.MORPH_OPEN, erode);
Imgproc.Canny(roi, roi, 150, 150 * 3, 3, true);
Vector<Rect> letters = detectionPlateCharacterContour(roi);
doTesseractOCR(letters, roiBinarize);
private static void doTesseractOCR(Vector<Rect> letters, Mat plate){
Tesseract instance = new Tesseract(); //
instance.setLanguage(LANGUAGE);
String resultPlate = "";
for(int i= 0; i < letters.size(); i++){
BufferedImage letter = OpenCvUtils.Mat2bufferedImage(plate.submat(letters.get(i)));
try {
String result = instance.doOCR(letter);
resultPlate += result + " position "+i;
} catch (TesseractException e) {
System.err.println(e.getMessage());
}
System.out.println("Tesseract output: "+resultPlate);
}
}
private static Vector<Rect> detectionPlateCharacterContour(Mat roi) {
Mat contHierarchy = new Mat();
Mat imageMat = roi.clone();
Rect rect = null;
List<MatOfPoint> contours = new ArrayList<>();
Imgproc.findContours(imageMat, contours, contHierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE);
Vector<Rect> rect_array = new Vector<>();
for (int i = 0; i < contours.size(); i++) {
rect = Imgproc.boundingRect(contours.get(i));
double ratio = 0;
if(rect.height > rect.width){
ratio = rect.height/rect.width;
}else{
ratio = rect.width/rect.height;
}
Logger.printMessage("Ratio of letter: "+ratio);
double contourarea = Imgproc.contourArea(contours.get(i));
if (contourarea >= 100 && contourarea <= 1000 && ( ratio >= 1 && ratio <= 2)) {
Imgproc.rectangle(roiColor, rect.br(), rect.tl(), new Scalar(255,0,0));
rect_array.add(rect);
}
}
contHierarchy.release();
return rect_array;
}
I have used find contours and boundingrect and display it at my project. then I want to find the largest contours and display it. Is this possible? I am newbie to OpenCV java lang.
heres my code so far:
#Override
public void onCameraViewStarted(int width, int height) {
mRgba = new Mat(height, width, CvType.CV_8UC4);
mHsv = new Mat(height,width,CvType.CV_8UC3);
hierarchy = new Mat();
mHsvMask = new Mat();
mDilated = new Mat();
mEroded = new Mat();
}
#Override
public void onCameraViewStopped() {
mRgba.release();
mHsv.release();
mHsvMask.release();
mDilated.release();
hierarchy.release();
}
#Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
mRgba =inputFrame.rgba();
contours = new ArrayList<MatOfPoint>();
hierarchy =new Mat();
mHsv = new Mat();
mHsvMask =new Mat();
Imgproc.cvtColor(mRgba, mHsv, Imgproc.COLOR_RGB2HSV);
Scalar lowerThreshold = new Scalar ( 0, 0, 0 ); // Blue color – lower hsv values
Scalar upperThreshold = new Scalar ( 179, 255, 10 ); // Blue color – higher hsv values
Core.inRange ( mHsv, lowerThreshold , upperThreshold, mHsvMask );
//just some filter
//Imgproc.dilate ( mHsvMask, mDilated, new Mat() );
//Imgproc.erode(mDilated,mEroded,new Mat());
Imgproc.findContours(mHsvMask, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);
for ( int contourIdx=0; contourIdx < contours.size(); contourIdx++ )
{
//Minimun size allowed for consideration
MatOfPoint2f approxCurve = new MatOfPoint2f();
MatOfPoint2f contour2f = new MatOfPoint2f(contours.get(contourIdx).toArray());
//Processing on mMOP2f1 which is in type MatOfPoint2f
double approxDistance = Imgproc.arcLength(contour2f,true)*0.02;
Imgproc.approxPolyDP(contour2f,approxCurve,approxDistance,true);
//convert to MatofPoint
MatOfPoint point = new MatOfPoint(approxCurve.toArray());
//get boundingrect from contour
Rect rect = Imgproc.boundingRect(point);
Imgproc.rectangle(mRgba,new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(255, 0, 0, 255),3);
//bisa Imgproc.rectangle(mRgba, rect.tl(), rect.br(), new Scalar(255, 0, 0),1, 8,0);
//show contour kontur
if(Imgproc.contourArea(contours.get(contourIdx))>100) {
Imgproc.drawContours(mRgba, contours, contourIdx, new Scalar(0,255,0), 5);
}
}
return mRgba;
Hopefully, someone has some experience in this. Thanks..
With function Imgproc.contourArea you can just simply find the areas of all of your contours and the contour with the largest area would simply be the largest one.
Code to draw the largest contour would be like this:
double maxVal = 0;
int maxValIdx = 0;
for (int contourIdx = 0; contourIdx < contours.size(); contourIdx++)
{
double contourArea = Imgproc.contourArea(contours.get(contourIdx));
if (maxVal < contourArea)
{
maxVal = contourArea;
maxValIdx = contourIdx;
}
}
Imgproc.drawContours(mRgba, contours, maxValIdx, new Scalar(0,255,0), 5);
I have been having some issues getting the outline of the detected object in the correct place, its as if the coordinates are in the wrong place. I have the hessian set to 2000 and I have filtered for matches that are less than 3 times the minimum distance. Any help would be appreciated.
Results from running matching and homography:
Code sample below:
public static void findMatches()
{
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
//Load Image 1
Mat img_object = Highgui.imread("./resources/Database/box.png");
//Load Image 2
Mat img_scene = Highgui.imread("./resources/Database/box_in_scene.png");
//Check if either image is null if so exit application
if (img_object == null || img_scene == null)
{
System.exit(0);
}
//Convert Image 1 to greyscale
Mat grayImageobject = new Mat(img_object.rows(), img_object.cols(), img_object.type());
Imgproc.cvtColor(img_object, grayImageobject, Imgproc.COLOR_BGRA2GRAY);
Core.normalize(grayImageobject, grayImageobject, 0, 255, Core.NORM_MINMAX);
//Convert image 2 to greyscale
Mat grayImageScene = new Mat(img_scene.rows(), img_scene.cols(), img_scene.type());
Imgproc.cvtColor(img_scene, grayImageScene, Imgproc.COLOR_BGRA2GRAY);
Core.normalize(grayImageScene, grayImageScene, 0, 255, Core.NORM_MINMAX);
//Create a SURF feature detector
FeatureDetector detector = FeatureDetector.create(4); //4 = SURF
//Cannot input hessian value as normal so we have to write the desired value into a
//file and then read value from file into detector.read
try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("hessian.txt"), "utf-8"))) {
writer.write("%YAML:1.0\nhessianThreshold: 2000.\noctaves:3\noctaveLayers: 4\nupright: 0\n");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
detector.read("hessian.txt");
//Mat of keypoints for object and scene
MatOfKeyPoint keypoints_object = new MatOfKeyPoint();
MatOfKeyPoint keypoints_scene = new MatOfKeyPoint();
//Detect keypoints in scene and object storing them in mat of keypoints
detector.detect(img_object, keypoints_object);
detector.detect(img_scene, keypoints_scene);
DescriptorExtractor extractor = DescriptorExtractor.create(2); //2 = SURF;
Mat descriptor_object = new Mat();
Mat descriptor_scene = new Mat() ;
extractor.compute(img_object, keypoints_object, descriptor_object);
extractor.compute(img_scene, keypoints_scene, descriptor_scene);
DescriptorMatcher matcher = DescriptorMatcher.create(1); // 1 = FLANNBASED
MatOfDMatch matches = new MatOfDMatch();
matcher.match(descriptor_object, descriptor_scene, matches);
List<DMatch> matchesList = matches.toList();
Double max_dist = 0.0;
Double min_dist = 100.0;
for(int i = 0; i < descriptor_object.rows(); i++){
Double dist = (double) matchesList.get(i).distance;
if(dist < min_dist) min_dist = dist;
if(dist > max_dist) max_dist = dist;
}
System.out.println("-- Max dist : " + max_dist);
System.out.println("-- Min dist : " + min_dist);
LinkedList<DMatch> good_matches = new LinkedList<DMatch>();
MatOfDMatch gm = new MatOfDMatch();
for(int i = 0; i < descriptor_object.rows(); i++){
if(matchesList.get(i).distance < 3*min_dist){
good_matches.addLast(matchesList.get(i));
}
}
gm.fromList(good_matches);
Mat img_matches = new Mat();
Features2d.drawMatches(img_object,keypoints_object,img_scene,keypoints_scene, gm, img_matches, new Scalar(255,0,0), new Scalar(0,0,255), new MatOfByte(), 2);
if(good_matches.size() >= 10){
LinkedList<Point> objList = new LinkedList<Point>();
LinkedList<Point> sceneList = new LinkedList<Point>();
List<KeyPoint> keypoints_objectList = keypoints_object.toList();
List<KeyPoint> keypoints_sceneList = keypoints_scene.toList();
for(int i = 0; i<good_matches.size(); i++){
objList.addLast(keypoints_objectList.get(good_matches.get(i).queryIdx).pt);
sceneList.addLast(keypoints_sceneList.get(good_matches.get(i).trainIdx).pt);
}
MatOfPoint2f obj = new MatOfPoint2f();
obj.fromList(objList);
MatOfPoint2f scene = new MatOfPoint2f();
scene.fromList(sceneList);
Mat homography = Calib3d.findHomography(obj, scene);
Mat obj_corners = new Mat(4,1,CvType.CV_32FC2);
Mat scene_corners = new Mat(4,1,CvType.CV_32FC2);
obj_corners.put(0, 0, new double[] {0,0});
obj_corners.put(1, 0, new double[] {img_object.cols(),0});
obj_corners.put(2, 0, new double[] {img_object.cols(),img_object.rows()});
obj_corners.put(3, 0, new double[] {0,img_object.rows()});
//Compute the most probable perspective transformation
//out of several pairs of corresponding points.
//Imgproc.getPerspectiveTransform(obj_corners, scene_corners);
Core.perspectiveTransform(obj_corners,scene_corners, homography);
Core.line(img_matches, new Point(scene_corners.get(0,0)), new Point(scene_corners.get(1,0)), new Scalar(0, 255, 0),4);
Core.line(img_matches, new Point(scene_corners.get(1,0)), new Point(scene_corners.get(2,0)), new Scalar(0, 255, 0),4);
Core.line(img_matches, new Point(scene_corners.get(2,0)), new Point(scene_corners.get(3,0)), new Scalar(0, 255, 0),4);
Core.line(img_matches, new Point(scene_corners.get(3,0)), new Point(scene_corners.get(0,0)), new Scalar(0, 255, 0),4);
Highgui.imwrite("./resources/ImageMatching" + ".jpg", img_matches);
createWindow("Image Matching", "resources/ImageMatching.jpg");
}
else
{
System.out.println("Not enough Matches");
System.exit(0);
}
}
The coordinates are in the correct place, you're simply drawing on the wrong image.
Your coordinates are relative to the second image img_scene . So if you draw your lines only on that image they'll be correct.
If you want to draw the lines on a composed image, where img_scene is translated to the right by the width of the first image img_object, you simply need to add img_object.cols() to the points x coordinates.
For example:
Core.line(img_matches,
new Point(scene_corners.get(0,0)[0] + img_object.cols(), scene_corners.get(0,0)[1]),
new Point(scene_corners.get(1,0)[0] + img_object.cols(), scene_corners.get(1,0)[1]),
new Scalar(0, 255, 0),4);
for the first line, and the same for next 3 lines.