Ask Your Question

c++: 4 channel image. How to trim edges with transparency = 0?

asked 2018-10-18 06:52:08 -0500

mohsene gravatar image

updated 2020-10-29 17:28:48 -0500

I have a 4 channel image (png), what is the best way to trim away the edges that has 0 transparency with performance in mind?

image description image description

image description


edit retag flag offensive close merge delete


Can you upload the image with actual transparency?

sjhalayka gravatar imagesjhalayka ( 2018-10-18 17:25:38 -0500 )edit

not really an opencv problem.

berak gravatar imageberak ( 2018-10-19 01:37:07 -0500 )edit

@sjhalayka I uploaded the actual image with transparency. @berak In a complex distributed system it is a bad choice to use multiple libraries for image Computer Vision and Image processing (ImageMagick can do trimming). But since OpenCV is sophisticated enough to pretty much implement anything to an image, why not use it for all purposes at once, if you have an implementation idea for trimming go ahead and share it does not have to be code.

mohsene gravatar imagemohsene ( 2018-10-19 03:55:55 -0500 )edit

@mohsene, -- noone cares about image transparency in computer-vision.

berak gravatar imageberak ( 2018-10-19 03:58:38 -0500 )edit

@berak I didn't know that this community helps depending on "personal interests". thanks for leaving your helpful comments ๐Ÿ‘Œ๐Ÿผ

mohsene gravatar imagemohsene ( 2018-10-19 04:05:21 -0500 )edit

@mohsene -- Good morning LOL. Your best bet is to start by scanning each row. Do you know how to do that in C++/OpenCV?

sjhalayka gravatar imagesjhalayka ( 2018-10-19 09:19:30 -0500 )edit

@mohsene, what have you tried so far, on your own ? (anything about "complex distributed system" is just hot air, if your skills did not allow to try anything (please at least proove, you did !), and you're only fishing for someone to write your code.)

berak gravatar imageberak ( 2018-10-19 10:32:05 -0500 )edit

@mohsene, -- btw, that's you ?

again -- you HAVE TO try something on you own, before asking questions, no ?

berak gravatar imageberak ( 2018-10-19 12:06:57 -0500 )edit

@berak I was expecting you when I first wrote this question; I've read many questions in this community and I did try multiple solutions but they weren't that efficient (one for instance is accessing each pixel which is a bad idea).

I know that you are the type of guy who humiliates who you don't like (i've seen your responses in this community you are kind of popular for that in here). I did not ask for code. even a hint would be much appreciated to lead me to do the task myself. at the end of the day thanks for everyone and just because you have knowledge does not mean you treat people (starters) like that and call them morons.

mohsene gravatar imagemohsene ( 2018-10-19 17:05:26 -0500 )edit

@berak sir, distributed systems (microservices) are not hot air, it is a very efficient and effective way of building a complex system in smaller components that are reusable and easy to deploy on their own and all connected together to handle massive user load. read more about Kubernetes, Spinnaker and Docker and how they play together to achieve that. deploying a secondary image processing/computer vision system beside OpenCV would be an over kill for such system. that is why I mentioned that I had no other choice but to implement trimming using OpenCV keeping in mind that OpenCV in my case is used for computer vision purposes as well such as nudity detection, facial recognition and color extraction.

mohsene gravatar imagemohsene ( 2018-10-19 17:33:21 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-10-19 12:10:06 -0500

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"

int main()
    std::vector<cv::Mat> channels;

    cv::Mat img = cv::imread("15399391149075274.png", cv::IMREAD_UNCHANGED);

    if (img.channels() == 4)
        cv::split(img, channels);               // seperate channels
        cv::Rect r = boundingRect(channels[3]); // find boundingRect of alpha channel
        cv::imwrite("result.png", img(r));
        printf("result.png saved!");

    return 0;

result.png :

image description

edit flag offensive delete link more


@sturkmen -- sweet. but you can't do anything about a moron, not being able to code himself out of a paper bag here. (and, imho, helping those for cheap, won't make this world a better place)

berak gravatar imageberak ( 2018-10-19 12:38:47 -0500 )edit

(yea, i'm grumpy)

berak gravatar imageberak ( 2018-10-19 12:47:10 -0500 )edit

no matter. i think at least @sjhalayka learned something new.

sturkmen gravatar imagesturkmen ( 2018-10-19 13:06:41 -0500 )edit

by the way if @sjhalayka recover his deleted answer will be better because sometimes seeing a wrong answers is also good.

sturkmen gravatar imagesturkmen ( 2018-10-19 13:32:43 -0500 )edit

Not sure how to undelete an answer. :(

sjhalayka gravatar imagesjhalayka ( 2018-10-19 14:42:36 -0500 )edit

hmm. i think it is not possible now. ( i think it was possible in past before upgrading Askbot )

sturkmen gravatar imagesturkmen ( 2018-10-19 15:38:36 -0500 )edit

Question Tools

1 follower


Asked: 2018-10-18 06:52:08 -0500

Seen: 1,731 times

Last updated: Oct 19 '18