JavaScript programming through Matlab centroid algorithm positioning learning

JavaScript programming through Matlab centroid algorithm positioning learning

As a closed commercial software, Matlab is controlled by the US government and ignores business ethics, so it is not recommended for use. If you like Matlab syntax, you can move to the open source octave, which has the same syntax as Matlab.

Matlab Centroid Algorithm

The so-called centroid is the center of gravity when the density is used as the grayscale value of the pixel. For example, the x coordinate of the centroid is

insert image description here

The most intuitive method is the following one.

%%Find the center of mass position of img through the center of mass algorithm function [x,y] = oCenter(img)
img = double(img);
[m,n] = size(img);
x = 0;y = 0;sum=0;
for i = 1:m
    for j = 1:n
        y = y + img(i,j)*i;
        x = x + img(i,j)*j;
        sum = sum+img(i,j);
    end
end
x = x/sum;
y = y/sum;

This is simple and crude enough, but it is too ugly. After all, in Matlab, matrix is ​​the most basic operation unit.
And in the process of accumulating and summing, the same array is indeed used repeatedly. For the i-th row, each column is multiplied by 1,2,3... and summed, which is the dot product of the i-th row vector and the vector [1:n] . So regardless of the entire picture, the centroid of the i-th row vector can be written out relatively simply.

x = img(i,:)*(1:n)'/sum(img(i,:));

Based on this, we also got an unexpected benefit, that is, we can easily write the centroid of each row in one line of expression

x = img*(1:n)'./sum(img,2);%The centroid of each row y = (1:m)*img./sum(img);%The centroid of each column

OCD means looking comfortable.
Accordingly, the centroid of the entire image can be written as

sumImg = sum(img(:));
x = sum(img)*(1:n)'/sumImg;
y = (1:m)*sum(img,2)/sumImg;

The above is the detailed content of JavaScript programming through Matlab centroid algorithm positioning learning. For more information about JavaScript positioning Matlab centroid algorithm, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • JS interview questions --- questions about algorithm steps
  • Using crypto-js AES symmetric encryption algorithm in Vue to implement encryption and decryption
  • Summary of seven sorting algorithms implemented in JavaScript (recommended!)
  • A brief discussion on an efficient algorithm for constructing tree structures in JavaScript
  • Implementation code of multi-level sorting algorithm in JS
  • How to implement simple calendar algorithm through JS
  • JavaScript Algorithm Interview Questions

<<:  Specific example of MySQL multi-table query

>>:  How to deploy and start redis in docker

Recommend

How to add a column to a large MySQL table

The question is referenced from: https://www.zhih...

How to set Tomcat as an automatically started service? The quickest way

Set Tomcat to automatically start the service: I ...

Teach you to implement a simple promise step by step

Table of contents Step 1: Build the framework Ste...

How to use docker+devpi to build local pypi source

Some time ago, I needed to use pip downloads freq...

Problems with using multiple single quotes and triple quotes in MySQL concat

When dynamically concatenating strings, we often ...

Recommend several MySQL related tools

Preface: With the continuous development of Inter...

Detailed tutorial on installing Docker on CentOS 8

1. Previous versions yum remove docker docker-cli...

MySQL 8.0 Window Function Introduction and Summary

Preface Before MySQL 8.0, it was quite painful to...

Example explanation of MySQL foreign key constraints

MySQL's foreign key constraint is used to est...

JavaScript to achieve mouse tailing effect

Mouse effects require the use of setTimeout to ge...

JavaScript object built-in objects, value types and reference types explained

Table of contents Object Object Definition Iterat...

Calendar effect based on jQuery

This article example shares the specific code of ...

Key knowledge summary of Vue development guide

Table of contents Overview 0. JavaScript and Web ...

HTML is something that web page creators must learn and master.

What are the benefits of learning HTML? 1: Easily...