Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions 4_cv_basics/10_face_swap/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Specifying which compiler to use
CC = g++

# Defining the name of the object that will be created after using make
PROJECT = image_segmentation

# Include directories and compiler flags
CFLAGS = -std=c++11 $(shell pkg-config --cflags opencv4) -Iinclude/

# Linker flags for OpenCV
LIBS = $(shell pkg-config --libs opencv4)

# Check if link is set
ifeq ($(SRC), $(link),)
build:
$(error "SRC is not set")
else
build:
@echo "Building..."
@$(CC) $(SRC) $(link) -o $(PROJECT) $(CFLAGS) $(LIBS)
endif

# if folder is not set, clean all build files all subfolders
.PHONY: clean
clean:
@echo "Cleaning..."
@rm -rf $(PROJECT)
11 changes: 11 additions & 0 deletions 4_cv_basics/10_face_swap/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
### Instructions for testing

```
make SRC=main.cpp link="src/segementation.cpp src/processing.cpp"\
```
then for running it
```

./image_segmentation

```
Binary file added 4_cv_basics/10_face_swap/assets/image1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 4_cv_basics/10_face_swap/image_segmentation
Binary file not shown.
53 changes: 53 additions & 0 deletions 4_cv_basics/10_face_swap/include/processing.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
MIT License

Copyright (c) 2026 Society of Robotics and Automation

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#ifndef PROCESSING_HPP
#define PROCESSING_HPP

#include "segmentation.hpp"
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <string>

cv::Mat segmentObject(ImageSegmenter& segmenter, const cv::Mat& image, const std::string& message);

cv::Mat createMask(const cv::Mat& segmented);

int kernel_sum(const cv::Mat& source_image, int row, int col, int kernel_size);

cv::Mat erosion(cv::Mat source_image, cv::Mat output_image, int kernel_size);

cv::Mat dilation(cv::Mat source_image, cv::Mat output_image, int kernel_size);

cv::Mat smoothMask(const cv::Mat& inputMask);

void extractForeground(const cv::Mat& image, const cv::Mat& mask, cv::Mat& foreground, cv::Mat& croppedMask, cv::Rect& boundingBox);

void resizeObject(cv::Mat& object, cv::Mat& mask, const cv::Size& targetSize);

cv::Mat removeOriginalObjects(const cv::Mat& image, const cv::Mat& mask1, const cv::Mat& mask2, const cv::Rect& r1, const cv::Rect& r2);

void pasteSwappedObjects(cv::Mat& baseImage, const cv::Mat& fg1, const cv::Mat& fg2, const cv::Mat& m1, const cv::Mat& m2, const cv::Rect& r1, const cv::Rect& r2);

#endif
55 changes: 55 additions & 0 deletions 4_cv_basics/10_face_swap/include/segmentation.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
MIT License

Copyright (c) 2023 Society of Robotics and Automation

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/


#ifndef SEGMENTATION_HPP
#define SEGMENTATION_HPP

#include <opencv2/opencv.hpp>

class ImageSegmenter {
public:
ImageSegmenter();

// GrabCut segmentation
void applyGrabCut(const cv::Mat& image, cv::Mat& output, const cv::Rect& roi = cv::Rect());

// Utility methods
cv::Rect selectROI(const cv::Mat& image);

private:
// Mouse callback data
struct MouseData {
cv::Mat image;
cv::Rect roi;
bool isSelecting;
};

MouseData mouseData;

// Mouse callback function
static void onMouse(int event, int x, int y, int flags, void* userdata);
};

#endif // SEGMENTATION_HPP
98 changes: 98 additions & 0 deletions 4_cv_basics/10_face_swap/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@

/*
MIT License

Copyright (c) 2026 Society of Robotics and Automation

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "processing.hpp"
#include <opencv2/imgcodecs.hpp>
#include <iostream>
#include <stdexcept>

int main()
{
try
{
cv::Mat image = cv::imread("assets/image1.png");
if (image.empty())
throw std::runtime_error("Could not load image");

ImageSegmenter segmenter;

cv::Mat fg1, fg2, m1, m2;
cv::Rect r1, r2;
cv::Mat mask1, mask2;

// Loop until valid segmentation for object 1
while (true)
{
try
{
cv::Mat segmented1 = segmentObject(segmenter, image, "Select first ROI and press any key...");//segment object and get segmented image
mask1 = smoothMask(createMask(segmented1)); //create mask and smooth it
extractForeground(image, mask1, fg1, m1, r1);//extract foreground, mask, and bounding box
break;
}
catch (const std::exception& e)
{
std::cerr << "First object segmentation failed: " << e.what() << ". Try again.\n";
}
}

// Loop until valid segmentation for object 2
while (true)
{
try
{
cv::Mat segmented2 = segmentObject(segmenter, image, "Select second ROI and press any key...");//segment object and get segmented image
mask2 = smoothMask(createMask(segmented2));// create mask and smooth it
extractForeground(image, mask2, fg2, m2, r2);//extract foreground, mask, and bounding box
break;
}
catch (const std::exception& e)
{
std::cerr << "Second object segmentation failed: " << e.what() << ". Try again.\n";
}
}

resizeObject(fg1, m1, r2.size());//resize foreground and mask of object 1 to match size of object 2
resizeObject(fg2, m2, r1.size());//resize foreground and mask of object 2 to match size of object 1

cv::Mat swapped = removeOriginalObjects(image, mask1, mask2, r1, r2);//remove original objects from the image and get the base for swapping

pasteSwappedObjects(swapped, fg1, fg2, m1, m2, r1, r2);//paste the swapped objects onto the base image

cv::imshow("Original", image);// show the original image
cv::imshow("Segmented SWAP", swapped);//show the swaped image
cv::waitKey(0);
cv::destroyAllWindows();

std::cout << "Object swap completed successfully. Exiting..." << std::endl;
}
catch (const std::exception& e)
{
std::cerr << "Error: " << e.what() << std::endl;
return -1;
}

return 0;
}
Loading
Loading