Giter Site home page Giter Site logo

Comments (6)

Aliktk avatar Aliktk commented on June 16, 2024 1

Thank you. Got your logic.
hat of to you 🥉

from copy_move_forgery_detection.

cantugba avatar cantugba commented on June 16, 2024

Hi @Aliktk,

Ransac is used on line 42 (Detector/MatchFeature/Match.py) to remove incorrect matches. With the abstract class(refer this: Detector/AbstractDetector.py - constructer method), these matches are kept in the list and the matching algorithm is also used. So yes Ransac is included in that. to confirm this, i have removed ransac and checked for matching points.

There are no special options in Gui for ransac. GUI shows that matching points directly using ransac

Regards,
Tugba

from copy_move_forgery_detection.

Aliktk avatar Aliktk commented on June 16, 2024

Thank you for your quick response. I appreciate that.
I added two more algorithms, AHC and RANSAC, for clustering. I am another question what if I want to to plot Rectangle on SURF, SWIFT, and AKAZE and want to apply lines during detection? I change the code in the AbstractDetector.py as below:

from abc import ABCMeta, abstractmethod

from Detector.MatchFeature.Match import MatchFeatures
from DrawFunctions.Rectangle import DrawRectangle
from DrawFunctions.Line import DrawLine

# from DrawFunctions.Circle import DrawCircle
class AbstractDetector(metaclass=ABCMeta):
    key_points = None
    descriptors = None
    color = None
    image = None
    distance = None
    MatchFeatures = None
    Draw = None

    def __init__(self, image):
        self.image = image
        self.MatchFeatures = MatchFeatures(self.key_points, self.descriptors, self.distance)  # match points
        self.Draw = DrawRectangle(self.image, self.MatchFeatures.gPoint1, self.MatchFeatures.gPoint2, self.color, self.MatchFeatures.cRectangle)  # draw matches
        self.Draw = DrawLine(self.image,  self.MatchFeatures.gPoint1,  self.MatchFeatures.gPoint2, self.color) # from DrawFunctions.Line import DrawLine -> import it
        self.Draw = DrawCircle(self.image, self.MatchFeatures.gPoint1, self.MatchFeatures.gPoint2, self.color) # from DrawFunctions.Circle import DrawCircle -> import it
        self.image = self.Draw.image

    # detect keypoints and descriptors
    @abstractmethod
    def detectFeature(self):
        pass
    
    
    def visualize(self, key_points1, key_points2):
        shape_cls = self.visualization_shape()
        shape_cls(self.image, key_points1, key_points2, self.color)
        
    @abstractmethod
    def visualization_shape(self):
        raise NotImplementedError("Subclasses should implement this method to return the correct drawing class")

also, add function for lines and rectangles for specified files as follow:

    def visualization_shape(self):
        return DrawRectangle

Required Task:

I want to add new features like applying SIFT, SURF, and AKAZE will print the rectangle, and on AHC and RANSAC, it will print lines on the image.
is it possible?

from copy_move_forgery_detection.

cantugba avatar cantugba commented on June 16, 2024

Yes its possible..You can create another class for AHC and RANSAC. Instead of using the abstract class, you can make your own settings separately for draw or matching algorithms.

from copy_move_forgery_detection.

Aliktk avatar Aliktk commented on June 16, 2024

sorry to bother you again. actually I already added them and it's been applying but don't have enough understanding of the whole entire code so that i would print lines or clustring type detection result in image.
here is the code for AHCDetector;

import cv2
import numpy as np
from sklearn.cluster import AgglomerativeClustering
from Detector.AbstractDetector import AbstractDetector

class AHCDetector(AbstractDetector):
    def __init__(self, image):
        self.image = image.copy()
        self.detectFeature()
        self.applyClustering()
        super().__init__(self.image)

    def detectFeature(self):
        # Using SIFT for demonstration purposes.
        sift = cv2.SIFT_create()
        gray = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY)
        self.key_points, self.descriptors = sift.detectAndCompute(gray, None)

    def applyClustering(self, threshold=5):
        # Prepare data for clustering
        points = np.array([kp.pt for kp in self.key_points])
        
        # Clustering
        clustering = AgglomerativeClustering(n_clusters=None, distance_threshold=threshold)
        clustering.fit(points)

        for cluster_id in set(clustering.labels_):
            if list(clustering.labels_).count(cluster_id) > threshold:
                cluster_points = [kp.pt for idx, kp in enumerate(self.key_points) if clustering.labels_[idx] == cluster_id]
                
                # Draw circles around each keypoint in the cluster
                for pt in cluster_points:
                    cv2.circle(self.image, tuple(map(int, pt)), 5, (0, 255, 0), -1)  # 5 is the radius of the circle

Here is the code for RANSAC:

import cv2
import numpy as np
from sklearn.cluster import AgglomerativeClustering
from Detector.AbstractDetector import AbstractDetector

class RansacDetector(AbstractDetector):
    def __init__(self, image):
        self.image = image.copy()
        self.detectFeature()
        self.matches = self.match_features(self.descriptors, self.descriptors)
        self.detectForgery(self.matches)
        super().__init__(self.image)

    def detectFeature(self):
        sift = cv2.SIFT_create()
        gray = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY)
        self.key_points, self.descriptors = sift.detectAndCompute(gray, None)

    def match_features(self, desc1, desc2, distance_ratio=0.7):
        FLANN_INDEX_KDTREE = 0
        index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
        search_params = dict(checks=50)
        flann = cv2.FlannBasedMatcher(index_params, search_params)
        matches = flann.knnMatch(desc1, desc2, k=2)
        good_matches = [m for m, n in matches if m.distance < distance_ratio * n.distance]
        return good_matches
    
    def detectForgery(self, matches, threshold=5):
        # Prepare data for clustering
        points = np.array([(self.key_points[match.queryIdx].pt, self.key_points[match.trainIdx].pt) for match in matches])
        points = points.reshape(points.shape[0], -1)

        # Clustering
        clustering = AgglomerativeClustering(n_clusters=None, distance_threshold=threshold)
        clustering.fit(points)

        for cluster_id in set(clustering.labels_):
            if list(clustering.labels_).count(cluster_id) > threshold:
                src_pts = np.float32([self.key_points[match.queryIdx].pt for idx, match in enumerate(matches) if clustering.labels_[idx] == cluster_id]).reshape(-1,1,2)
                dst_pts = np.float32([self.key_points[match.trainIdx].pt for idx, match in enumerate(matches) if clustering.labels_[idx] == cluster_id]).reshape(-1,1,2)
                
                # RANSAC to identify affine transformations
                if len(src_pts) >= 4 and len(dst_pts) >= 4:  # Ensure there are at least 4 points to compute homography
                    M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
                    if mask.sum() > threshold:
                        # Forgery detected for this cluster
                        # Visualization: Draw lines connecting the clustered keypoints
                        for i, (src, dst) in enumerate(zip(src_pts, dst_pts)):
                            if mask[i]:
                                pt1, pt2 = tuple(map(int, src[0])), tuple(map(int, dst[0]))
                                cv2.line(self.image, pt1, pt2, (0, 255, 0), 2)

Please help me out there as I already code it and already the code is working but not getting the actuall results for drawing lines on the image via AHC and RANSAC algorithms,

Thank you for your cooperation

from copy_move_forgery_detection.

cantugba avatar cantugba commented on June 16, 2024

if you want to use specific draw function for Ransac or AHC remove from Detector.AbstractDetector import AbstractDetector
implementation for both class. It takes the drawing functions and matching from the base class.

You can add the drawing function here as well as adding MatchFeatures to the constructor method. It should be something like this;

import cv2
import numpy as np
from sklearn.cluster import AgglomerativeClustering
// from Detector.AbstractDetector import AbstractDetector

class AHCDetector(AbstractDetector):
def init(self, image):
self.image = image.copy()
self.detectFeature()
self.applyClustering()
// choose specific draws for your algorithm
self.Draw = DrawRectangle(self.image, self.MatchFeatures.gPoint1, self.MatchFeatures.gPoint2, self.color, self.MatchFeatures.cRectangle) # draw matches
self.image = self.Draw.image
//super().init(self.image)

regardingg drawing part:

with detectForgery(self, matches, threshold=5) method you need to return points for draw line -> return this points pt1, pt2 and you can keep it in a different list

then draw these points. You have already run the draw function in the abstract class.

from copy_move_forgery_detection.

Related Issues (3)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.