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

Solve the problem of IDEA configuring tomcat startup error

The following two errors were encountered when co...

Teach you how to implement the observer mode in Javascript

Table of contents What is the Observer Pattern? S...

CenOS6.7 mysql 8.0.22 installation and configuration method graphic tutorial

CenOS6.7 installs MySQL8.0.22 (recommended collec...

WeChat applet selects the image control

This article example shares the specific code for...

Vue realizes the card flip effect

This article example shares the specific code of ...

Vue implements sending emoticons in chat box

The specific code for sending emoticons in the vu...

How to install MySQL for beginners (proven effective)

1. Software Download MySQL download and installat...

Use Firebug tool to debug the page on iPad

How to debug a page on iPad? When using iOS 5, you...

Detailed explanation of Xshell common problems and related configurations

This article introduces common problems of Xshell...

js to implement web calculator

How to make a simple web calculator using HTML, C...

MySQL 5.7.18 winx64 installation and configuration method graphic tutorial

The installation of compressed packages has chang...

Detailed explanation of CSS3 text shadow text-shadow property

Text shadow text-shadow property effects: 1. Lowe...

MySQL 5.7.17 winx64 installation and configuration graphic tutorial

I summarized the previous notes on installing MyS...

Master-slave synchronization configuration of Mysql database

Table of contents Mysql master-slave synchronizat...

CSS to achieve compatible text alignment in different browsers

In the front-end layout of the form, we often nee...