# Extract the contour of two images

I tried the following code:


import cv2
import numpy as np

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:

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

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

edit retag close merge delete

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...

( 2018-11-19 08:35:11 -0500 )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...

( 2018-11-26 04:25:04 -0500 )edit

I have got the yellow line by using Photoshop.

( 2018-11-26 09:18:28 -0500 )edit

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

( 2018-11-26 09:35:55 -0500 )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?

( 2018-11-26 09:51:34 -0500 )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 ?

( 2018-11-28 12:18:58 -0500 )edit

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

( 2018-11-29 05:30:07 -0500 )edit

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

( 2018-11-29 05:34:56 -0500 )edit

Sort by ยป oldest newest most voted

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.

more

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

import cv2

# 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()

more

1

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

( 2018-11-19 08:55:46 -0500 )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.

( 2018-11-19 09:24:27 -0500 )edit

Official site

GitHub

Wiki

Documentation