2

I am using OpenCV and Python.

I want to remove the upper corner part of the image.

For example I have a image originally like this: enter image description here

Afterwards, I want to remove the upper triangle part.

enter image description here

Is there any method besides from "hard coding"?

VICTOR
  • 1,894
  • 5
  • 25
  • 54
  • 1
    I don't know what you mean by "hard coding", but you can either use `fillPoly` to draw a white triangle, or use a mask to do a copy of only the relevant bits. – Ken Y-N Jun 10 '16 at 02:39
  • Thank you for your reply. Can you explain more? Do you mean that I should first drawing a triangle using `fillPoly`. And then combine the triangle to my image? – VICTOR Jun 10 '16 at 02:42
  • 1
    The quickest way would be to load your image, then draw a white rectangle in the corner via `fillPoly`. I'm not that good with Python, so I cannot show any example code, but [here is an example](https://stackoverflow.com/questions/11270250/what-does-the-python-interface-to-opencv2-fillpoly-want-as-input) and [here another](https://stackoverflow.com/questions/17582849/opencv-cv2-fillpoly-vs-cv2-fillconvexpoly-expected-data-type-for-array-of-poly). – Ken Y-N Jun 10 '16 at 03:35

2 Answers2

3

Pedantically speaking, you cannot remove any part of an image except a stripe on either side, reducing it's width or height. For what looks like "removal of the corner part" you can draw a background-colored triangle over that corner part. Here, assuming your desired background color is white:

import cv2
import numpy
img = cv2.imread("zebra.jpg")
triangle = numpy.array([[300, 0], [399, 0], [399, 100]])
color = [255, 255, 255] #white
cv2.fillConvexPoly(img, triangle, color)
cv2.imwrite("with_triangle.jpg", img)

Original image

Image with a triangular part "removed"

For some purposes (blending, inserting into a web-page where the background is unknown etc.) you may want to use an image format which supports alpha-transparency (in your example you are using jpeg, which doesn't) and set the alpha channel to zero:

import cv2
import numpy
img = cv2.imread("zebra.jpg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA) #convert to 4-channel image
triangle = numpy.array([[300, 0], [399, 0], [399, 100]])
color = [255, 255, 255, 0] #white with zero alpha
cv2.fillConvexPoly(img, triangle, color)
cv2.imwrite("with_triangle.png", img) #save to PNG for transparency support
Headcrab
  • 6,838
  • 8
  • 40
  • 45
0

The answer above is "hard-coded", yet you have accepted it. You will have to manually change the values to create triangles for different images. Check below.

I have a solution that places a triangle by just varying a single variable var, depending on how big the triangle has to be. If you want you can fix a single value as well.

Just include var and alter the next line as shown:

var = 50
triangle = np.array([[img.shape[1] - var, 0], [img.shape[1], 0], [img.shape[1], var]])

Some samples:

Figure 1

enter image description here

Figure 2

enter image description here

Figure 3

enter image description here

Figure 4

enter image description here

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87