Ask Your Question
-1

Extract the contour of two images

asked 2018-11-19 07:49:11 -0600

Abdu gravatar image

updated 2018-11-24 09:34:30 -0600

I tried the following code:


import cv2
import numpy as np
img1 = cv2.imread("IMG_B_TestCube&1&00.png")
img2 = cv2.imread("IMG_TestCube.png")


h1, w1 = img1.shape[:2]
h2, w2 = img2.shape[:2]

# translation matrix of img2
MT = np.float32([[1,0,190],[0,1,-100]])
dst = cv2.warpAffine(img2,MT,(w2,h2))

#create empty matrix
vis = np.zeros((max(h1, h2), w1+w2,3), np.uint8)


#combine 2 images
vis[:h1, :w1, :3] = img1+dst
#vis[:h2, w1:w1+w2, :3] = img2


cv2.imshow('res',vis)
cv2.waitKey(0)
cv2.destroyAllWindows()

The result which I have got is the below:

image description

My new goal is to extract the yellow line of intersection as below:

image description I checked a lot of examples without finding an optimal solution.

Could you please help in this matter?

edit retag flag offensive close merge delete

Comments

2

Blending these two images will give you only two overlapped squares, no yellow region.

I don't know how you obtained the third image...

BTW, to extract a region of a specific color, use the inRange function...

kbarni gravatar imagekbarni ( 2018-11-19 08:35:11 -0600 )edit

I repeat my previous question: how did you get the second image with the yellow line? If you don't explain it, we won't be able to help you...

kbarni gravatar imagekbarni ( 2018-11-26 04:25:04 -0600 )edit

I have got the yellow line by using Photoshop.

Abdu gravatar imageAbdu ( 2018-11-26 09:18:28 -0600 )edit

I still don't understand... If you want to draw lines, use Paint, not OpenCV...

kbarni gravatar imagekbarni ( 2018-11-26 09:35:55 -0600 )edit

I don't want to draw the line using Paint. The yellow line in fact is the line of intersection of the green and white image. It is extracted just from the green image. Could it be done using opencv?

Abdu gravatar imageAbdu ( 2018-11-26 09:51:34 -0600 )edit

i seriously don't understand this question. the 1st image shows the (2d image) intersection of 3d boxes. why do you want ro use a 2d screenshot, to determine the intersection ?

berak gravatar imageberak ( 2018-11-28 12:18:58 -0600 )edit

@Abdu. By lookinng @ first image. Do you want to remove dark green rectangle and leave white image alone from first image?

supra56 gravatar imagesupra56 ( 2018-11-29 05:30:07 -0600 )edit

@berak. He probablies wanted to cropped out dark green not light green and not to cropped out piece of white image.

supra56 gravatar imagesupra56 ( 2018-11-29 05:34:56 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
0

answered 2018-11-28 06:39:09 -0600

kbarni gravatar image

If I understand you well (I'm still not sure about it), you need to do the following algorithm (this is just an outline):

1. Get the intersection of the shapes: intersect=alpha1&alpha2 (arithmetic AND operation)
2. Dilate the area using morphological operators
3. To get the yellow part, substract image1 from the result of the previous step.

Take care to use the alpha channel correctly when working with the images.

edit flag offensive delete link more
0

answered 2018-11-19 08:44:28 -0600

supra56 gravatar image

I didn't test it. I don't have colour images on it. So, you can try this in below.

import cv2 

# Read Image1 
mountain = cv2.imread('green.jpg, 1) 

# Read image2 
dog = cv2.imread('yellow.jpg, 1) 

# Blending the images with 0.3 and 0.7 
img = cv2.addWeighted(mountain, 0.3, dog, 0.7, 0) 

# Show the image 
cv2.imshow('image', img) 

# Wait for a key 
cv2.waitKey(0) 

# Distroy all the window open 
cv2.distroyAllWindows()
edit flag offensive delete link more

Comments

1

I think he should use alpha blending instead of a weighted sum.

kbarni gravatar imagekbarni ( 2018-11-19 08:55:46 -0600 )edit

@kbarni . Yes I have seem before. I have too many on my sd cards. That only 5 lines compared to 11 line from alpha blending from link.

supra56 gravatar imagesupra56 ( 2018-11-19 09:24:27 -0600 )edit

Question Tools

Stats

Asked: 2018-11-19 07:47:53 -0600

Seen: 419 times

Last updated: Nov 28 '18