title: Create a transparent image in opencv
body: <p>I am trying to rotate an image in x, y and z axis as in <a href="https://stackoverflow.com/questions/19093728/rotate-image-around-x-y-z-axis-in-opencv">this</a>.
  The image should not be cropped while rotating So I am doing <a href="https://stackoverflow.com/questions/22041699/rotate-an-image-without-cropping-in-opencv-in-c">this</a></p>
  
  <pre><code>Mat src = imread("path");
  int diagonal = (int)sqrt(src.cols*src.cols+src.rows*src.rows);
  int newWidth = diagonal;
  int newHeight =diagonal;
  
  Mat targetMat(newWidth, newHeight, src.type());
  </code></pre>
  
  <p>I am creating a bigger image targetMat. The input image is a png image.
  But I want this image as a transparent image. So I tried <a href="https://stackoverflow.com/questions/18491998/creating-transparent-image-in-opencv-c">this</a></p>
  
  <p><code>Mat targetMat(newWidth, newHeight, src.type(), cv::Scalar(0,0,0,0));</code></p>
  
  <p>But the output image was <a href="https://i.stack.imgur.com/NWuLt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NWuLt.png" alt="outputimage"></a></p>
  
  <p>What I need is <a href="https://i.stack.imgur.com/0KvB8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0KvB8.png" alt="transparent image"></a> (Transparent image is here)</p>
  
  <p>So what change do I have to do? </p>