I cannot perform color conversion after image rotation

Asked

Viewed 73 times

1

I’m developing an algorithm that rotates images every 10 degrees. For this, I am identifying the center of my region of interest, not the center of the image, because it has region of interest that are close to the corners. With this I can rotate each image centered, keeping the original image dimensions. The problem I’m having is that by identifying the center of the image, I convert the input image to grayscale, so I can’t convert the post-rotated image to the original color'.

Follows code below:

import cv2
import numpy as np

POS_ROT_STEP = 18
IMG = 'IMG006'

img = cv2.imread(IMG+'.png')

gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
_, contours,hierarchy = cv2.findContours(gray,2,1)
cnt = contours

for i in range (len(cnt)):
    (x,y),radius = cv2.minEnclosingCircle(cnt[i])
    center = (int(x),int(y))
    print ('Circle: ' + str(i) + ' - Center: ' + str(center) + ' - Radius: ' + str(radius))

# girando e cortando a imagem para produzir amostras sinteticas
for j in range(1, POS_ROT_STEP):
    (h, w) = img.shape[:2]

    rotated = cv2.getRotationMatrix2D(centro, -(360 / POS_ROT_STEP) * j, 1.0)

    nx = img.shape[1]
    ny = img.shape[0]

    rotated[0, 2] += (nx / 2) - x
    rotated[1, 2] += (ny / 2) - y

    output_aux = cv2.warpAffine(gray, rotated, (nx, ny))
    backtorgb = cv2.cvtColor(output_aux,cv2.COLOR_GRAY2RGB)
    cv2.imwrite('~/rotate/'+IMG+'-'+str( j )+'.png', backtorgb)

1 answer

2


After converting to gray, you lose all color information BGR. So it is not possible to perform the conversion from gray to colored.

What happens when you convert from grayscale for BGR with cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) is the creation of 3 channels, but all with equal values. Because it was grayscale.

Example

A pixel that has the following grayscale value: (127), after the conversion will have three channels: (127, 127, 127)

Alternative

An alternative to solve this problem is to save a copy of the original image with: copia = img.copy()

And after finding the coordinates on grayscale, work on the original image.

Observing

Opencv’s default color space is BGR, then when loading the image with img = cv2.imread(IMG+'.png'), she’s in the color space BGR.

Then the conversion gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY) should be gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

  • 1

    I used copy() and it worked, obgdo

Browser other questions tagged

You are not signed in. Login or sign up in order to post.